【问题标题】:404 on handsontable after moving it to resource folder将其移动到资源文件夹后在handsontable上出现404
【发布时间】:2021-04-26 17:38:04
【问题描述】:

我正在尝试在本地安装在可动手操作的 Spring Boot 应用程序中。下载后,我把handsontable移动到资源文件夹,注册了css和js文件的路径,但是浏览器一直返回找不到文件的错误。

handsontable:7 GET http://localhost/src/main/resources/static/handsontableV/dist/handsontable.full.min.js net::ERR_ABORTED 404

我的项目结构:

我的html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <title>Spring Security Example </title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link rel="stylesheet" type="text/css" href="src/main/resources/static/handsontableV/dist/handsontable.full.min.js" media="screen">
    <script src="src/main/resources/static/handsontableV/dist/handsontable.full.min.js"></script>
<!--    <script src="https://cdn.jsdelivr.net/npm/handsontable@8.2.0/dist/handsontable.full.min.js"></script>-->
<!--    <link href="https://cdn.jsdelivr.net/npm/handsontable@8.2.0/dist/handsontable.full.min.css" rel="stylesheet" media="screen">-->

    <link rel="stylesheet" href="/webjars/bootstrap/4.5.3/css/bootstrap.min.css" />
    <script src="/webjars/bootstrap/4.5.3/js/bootstrap.min.js"></script>
    <script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
</head>

网络配置:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("src/main/resources/static/handsontableV/dist/handsontable.full.min.js")
                .addResourceLocations("src/main/resources/static/handsontableV/dist/handsontable.full.min.js");

        registry
                .addResourceHandler("src/main/resources/static/handsontableV/dist/handsontable.full.min.css")
                .addResourceLocations("src/main/resources/static/handsontableV/dist/handsontable.full.min.css");

        registry
                .addResourceHandler("/webjars/**")
                .addResourceLocations("/webjars/");

    }
}

网络安全配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder(8);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
            .authorizeRequests()
            .antMatchers("/login", "/registration","/webjars/**", "/static/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
                .ignoring()
                .antMatchers("/resources/**");
    }

来自谷歌浏览器的回应:

我不明白。我尝试将路径更改为静态/handontableV/dist/handsontable.full.min.js

但它不起作用。什么原因可能spring看不到文件?

【问题讨论】:

  • 您的文件夹结构中有static.handsontable 而不是static/handsontable
  • 是的,idea 是这样写的,在 finder static / handsontable 中
  • 听起来很奇怪
  • 为什么你添加 src/main/resources/static 到脚本 url 如果 spring boot 已经默认使用资源/静态文件夹作为静态文件夹,我认为你应该使用这个 /handsontableV/dist/handsontable。脚本的 full.min.js url
  • 我也试过了,他还是写没看到文件

标签: javascript java spring-boot spring-mvc handsontable


【解决方案1】:

我决定创建一个 webapp 文件夹

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多