首页 技术 正文
技术 2022年11月15日
0 收藏 630 点赞 4,783 浏览 1328 个字

  最近在接触一个看公司的java后台项目(采用的耶鲁大学开源的一个cas单点登录系统),用的是框架是Spring Boot,用的模板是Thymeleaf,于是我生成一个Spring Boot的项目,并且引入Thymeleaf,开始了我的联系,我在引静态资源的时候总是报错:

No mapping found for HTTP request with URI [/jquery.min.js] in DispatcherServlet with name 'dispatcherServlet'

  我在网上查资料,得知Spring Boot对静态资源映射提供了默认配置(一般情况我们是不需要配置,除非我们要自定义路径映射),默认情况下,Spring Boot从类路径的以下位置提供静态资源:

  • /static
  • /public
  • /resources
  • /META-INF/resources

  Spring Boot 静态资源路径分析

  我的jquery.min.js也在classpath/static下啊,为啥找不到…找了半天,我发现我引了我们项目中的一个文件,这个文件修改默认的Spring Boot的配置,导致静态文件找不到:

  Spring Boot 静态资源路径分析

  文件内容如下:

package com.practice.springboot.hello.framework;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport { @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//将templates目录下的CSS、JS文件映射为静态资源,防止Spring把这些资源识别成thymeleaf模版
registry.addResourceHandler("/templates/**.js").addResourceLocations("classpath:/templates/");
registry.addResourceHandler("/templates/**.css").addResourceLocations("classpath:/templates/");
//其他静态资源
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}

  我将路径加上/static之后,http://localhost:8888/static/jquery.min.js就可访问到了.请求路径加上 /templates同理

  Spring Boot 静态资源路径分析

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898