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资源的报错问题的更多相关文章

  1. 擦他丫的,今天在Django项目中引用静态文件jQuery.js 就是引入报错,终于找到原因了!

    擦 ,今天在Django项目中引用静态文件jQuery.js 就是引入报错,终于找到原因了! 问题在于我使用的谷歌浏览器,默认使用了缓存,导致每次访问同一个url时,都返回的是缓存里面的东西.通过谷歌 ...

  2. js执行函数报错Cannot set property 'value' of null怎么解决?

    js执行函数报错Cannot set property 'value' of null 的解决方案: 原因:dom还没有完全加载 第一步:所以js建议放在body下面执行, 第二步:window.on ...

  3. SpringBoot注册Windows服务和启动报错的原因

    SpringBoot注册Windows服务和启动报错的原因 Windows系统启动Java程序会弹出黑窗口.黑窗口有几点不好.首先它不美观:其次容易误点导致程序关闭:但最让我匪夷所思的是:将鼠标光标选 ...

  4. 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 ...

  5. 在CentOS上安装node.js的时候报错:No acceptable C compiler found!解决办法

    在CentOS上安装node.js的时候报错:No acceptable C compiler found! 原因:没有c编译器. 解决办法:安装GCC 命令如下: #yum install gcc ...

  6. js&jquery避免报错的方法

      CreateTime--2016年12月8日15:28:40Author:Marydonjs&jquery规避报错信息的两种方式 <script type="text/ja ...

  7. 【问题记录】在执行js的时候报错:missing ) after argument list

    在执行个js语句时候报错: 报错语句: js('document.querySelector("[class] [tabindex='0']:nth-child(2) span") ...

  8. 【原】ajaxupload.js上传报错处理方法

    相信大家在工作中经常用到文件上传的操作,因为我是搞前端的,所以这里主要是介绍ajax在前端中的操作.代码我省略的比较多,直接拿js那里的 $.ajaxFileUpload({ url:'www.cod ...

  9. node.js创建服务器报错

    创建nodeTest.js如下: var http = require('http'); http.createServer(function (request, response){ respons ...

随机推荐

  1. 如何在文本编辑器中实现时间复杂度O(n/m)的搜索功能? BM算法

    //字符串匹配 public class StringCmp { //约定:A主串长 n ,B模式串 长m.要求:在A串中找到B串匹配的下标 //BM算法:从B串和A串尾部开始比较,希望一次将B串向后 ...

  2. Codeforces Round #606 (Div. 2)

    传送门 A. Happy Birthday, Polycarp! 签到. Code /* * Author: heyuhhh * Created Time: 2019/12/14 19:07:57 * ...

  3. MYSQL 命令导出事件、存储过程、触发器

    普通导出某个数据库 mysqldump -u username -p passowrd databasename > file.sql 顺便导出事件 使用 –events 参数 mysqldum ...

  4. wbr 标签实现连续英文字符的精准换行你知道吗?

    1.一般情况下的换行: word-break:break-all或者word-wrap:break-word <p>大家<wbr>想要<wbr>实现<wbr& ...

  5. 201871010116-祁英红《面向对象程序设计(java)》第十五周学习总结

    博文正文开头格式:(2分) 项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://ww ...

  6. 【Oracle】常用的SQL语句

    抄自:https://www.cnblogs.com/qiu18359243869/p/9474515.html 提示:dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里 ...

  7. C# 二维数组 转换成 DataTable

    C# 数据转换 Overview C# 窗体操作中,有些比较特别的操作.但是为了方便我们不得不使用一些比较特别的手段. C#中二维数组转DataTable 首先,我们看一下我对二维数组的数据处理.这次 ...

  8. A1060 Are They Equal (25 分)

    一.技术总结 cnta.cntb用于记录小数点出现的位置下标,初始化为strlen(字符串)长度. q.p用于记录第一个非0(非小数点)出现的下标,可以用于计算次方和方便统计输出的字符串,考虑到前面可 ...

  9. [Pytorch Bug] "EOFError: Ran out of input" When using Dataloader with num_workers=x

    在Windows上使用Dataloader并设置num_workers为一个非零数字,enumerate取数据时会引发"EOFError: Ran out of input"的报错 ...

  10. 【Collect】免费图片库网站推荐(国外高清可商用)

    #国外高清可商用免费图片库 1.https://unsplash.com/2.https://pixabay.com/3.https://www.sitebuilderreport.com/stock ...