SpringBoot关于静态js资源的报错问题

2019-12-02 09:45:01.636 WARN 9572 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound : No mapping for GET /static/css/main.css
2019-12-02 09:45:01.637 WARN 9572 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound : No mapping for GET /static/js/bootstrap.min.js
2019-12-02 09:45:01.700 WARN 9572 --- [nio-8080-exec-4] o.s.web.servlet.PageNotFound : No mapping for GET /static/js/bootstrap.min.js

出现了这些警告,是因为没有配置拦截器的缘故,

刚进公司 自己搭建springboot项目遇到这个错误
记录以后学习更多的知识和经验

springboot 使用thymeleaf 导致该错误

以下为配置文件application.properties

mybatis.mapper-locations=classpath:mappings/*.xml
mybatis.config-location=classpath:mybatis/mybatis-spring.xml

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ddd?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.timeout=10s
spring.redis.jedis.pool.min-idle=0

应该以什么样的路径来访问静态资源,这表示只有静态资源的访问路径为/static/ 时才会处理(如http://localhost:8080/static/css/base.css)
#spring.mvc.static-path-pattern= /static/**
#用于告诉Spring Boot应该在何处查找静态资源文件,查找文件时会依赖于配置的先后顺序依次进行
spring.resources.static-locations=classpath:/static/,classpath:/view/,classpath:/public,classpath:/resources,classpath:/META-INF/resources

spring.thymeleaf.prefix = classpath:/static/

#开发阶段,建议关闭thymeleaf的缓存
spring.thymeleaf.cache=false
#使用遗留的html5以去掉对html标签的校验
spring.thymeleaf.mode= HTML5
spring.thymeleaf.check-template = true
spring.thymeleaf.servlet.content-type = text/html
spring.thymeleaf.encoding = UTF-8
spring.thymeleaf.suffix = .html

springboot 使用thymeleaf 动态页面
跳转静态页面需要经过controller层才能实现跳转 (不经过静态资源报错)
否则 报 No mapping for GET错误

Spring Boot自动配置了classpath:/static/下面的资源为静态资源,后来网上找了很多的方法都试过了,解决不了。

于是我重新写了一个项目,把这个旧项目的配置一个一个的移动过去,最后发现是我配置的拦截器的问题。

因为我配置拦截器继承的类是:WebMvcConfigurationSupport这个类,它会让spring boot的自动配置失效。

 怎么解决呢?

    第一种可以继承WebMvcConfigurerAdapter,当然如果是1.8+WebMvcConfigurerAdapter这个类以及过时了,可以直接实现WebMvcConfigurer接口,然后重写addInterceptors来添加拦截器:

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInterceptor()).addPathPatterns("/user/**");
        WebMvcConfigurer.super.addInterceptors(registry);
    }
 
}

    或者还是继承WebMvcConfigurationSupport,然后重写addResourceHandlers方法:

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {
 
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInterceptor()).addPathPatterns("/user/**");
        super.addInterceptors(registry);
    }
     
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }
     
}

这里采用第二种方法,就把警告去掉了

同时因为警告,没办法找到jquery元素,$,jquery文件导入失败,也可以成功导入了。

猜你喜欢

转载自www.cnblogs.com/Koaler/p/11968937.html