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文件导入失败,也可以成功导入了。
SpringBoot关于静态js资源的报错问题的更多相关文章
- 擦他丫的,今天在Django项目中引用静态文件jQuery.js 就是引入报错,终于找到原因了!
擦 ,今天在Django项目中引用静态文件jQuery.js 就是引入报错,终于找到原因了! 问题在于我使用的谷歌浏览器,默认使用了缓存,导致每次访问同一个url时,都返回的是缓存里面的东西.通过谷歌 ...
- js执行函数报错Cannot set property 'value' of null怎么解决?
js执行函数报错Cannot set property 'value' of null 的解决方案: 原因:dom还没有完全加载 第一步:所以js建议放在body下面执行, 第二步:window.on ...
- SpringBoot注册Windows服务和启动报错的原因
SpringBoot注册Windows服务和启动报错的原因 Windows系统启动Java程序会弹出黑窗口.黑窗口有几点不好.首先它不美观:其次容易误点导致程序关闭:但最让我匪夷所思的是:将鼠标光标选 ...
- Please do not register multiple Pages in undefined.js 小程序报错的几种解决方案
Wed Jun 27 2018 09:25:43 GMT+0800 (中国标准时间) Page 注册错误,Please do not register multiple Pages in undefi ...
- 在CentOS上安装node.js的时候报错:No acceptable C compiler found!解决办法
在CentOS上安装node.js的时候报错:No acceptable C compiler found! 原因:没有c编译器. 解决办法:安装GCC 命令如下: #yum install gcc ...
- js&jquery避免报错的方法
CreateTime--2016年12月8日15:28:40Author:Marydonjs&jquery规避报错信息的两种方式 <script type="text/ja ...
- 【问题记录】在执行js的时候报错:missing ) after argument list
在执行个js语句时候报错: 报错语句: js('document.querySelector("[class] [tabindex='0']:nth-child(2) span") ...
- 【原】ajaxupload.js上传报错处理方法
相信大家在工作中经常用到文件上传的操作,因为我是搞前端的,所以这里主要是介绍ajax在前端中的操作.代码我省略的比较多,直接拿js那里的 $.ajaxFileUpload({ url:'www.cod ...
- node.js创建服务器报错
创建nodeTest.js如下: var http = require('http'); http.createServer(function (request, response){ respons ...
随机推荐
- JSON格式日期的转换
扒来的链接: https://blog.csdn.net/zhang33565417/article/details/99676975 感谢这位哥们儿的分享!
- 机器学习--matplotlib绘制各种图表
机器学习三剑客:numpy.pandas.matplotlib NumPy系统是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵. pandas 是基于numpy的一种工具,该工 ...
- linux下用sox音频处理常用方法
一 sox可以给pcm文件加头 方法:sox -t raw -c 1 -e signed-integer -b 16 -r 16000 test.pcm test.wav 二 修改采样率: 方法: s ...
- json数据格式与字典数据类型之间的相互转换
import json class HandleJson: ''' 定义一个json格式数据处理类 ''' @staticmethod def loads_data(data): ''' 将json数 ...
- 计算机组成原理——中央处理器(CPU)考研题
(一) CPU的功能和基本结构 (二) 指令执行过程 (三) 数据通路的功能和基本结构 (四) 控制器的功能和工作原理 1. 硬布线控制器2. 微程序控制器微程序.微指 ...
- Koa 中间件的执行
Node.js 中请求的处理 讨论 Koa 中间件前,先看原生 Node.js 中是如何创建 server 和处理请求的. node_server.js const http = require(&q ...
- frigate_TUNNEL
#coding=utf-8 Result=open('result.txt',"w") FileTunnel = open('tunnel.txt').readlines() Ne ...
- webpack基本使用
webpack安装时的坑 高版本的webpack除了全局安装webpack外,还需安装webpack-cli,在本地使用时也一样需要这样,不然会出错 webpack使用是的坑 在原始启动webpack ...
- python基础教程:dir()和__dict__属性的区别
只要是有属性的数据对象(不一定是面向对象的对象实例,而是指具有数据类型的数据对象),都可以通过- ---- __dict__和dir()来显示数据对象的相关属性. __ dict__可以看作是数据对象 ...
- hibernateHQL
HQL 即Hibernate Query Language的缩写 hql和sql区别/异同 HQL ...