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文件導入失敗,也可以成功導入了。