100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Spring Boot 实战(3)静态资源配置

Spring Boot 实战(3)静态资源配置

时间:2022-04-25 14:56:06

相关推荐

Spring Boot 实战(3)静态资源配置

写在前面: 我是「扬帆向海」,这个昵称来源于我的名字以及女朋友的名字。我热爱技术、热爱开源、热爱编程。技术是开源的、知识是共享的。

这博客是对自己学习的一点点总结及记录,如果您对Java、算法感兴趣,可以关注我的动态,我们一起学习。

用知识改变命运,让我们的家人过上更好的生活

相关文章:

Springboot 系列文章

文章目录

一、springmvc 对静态资源的映射规则二、SpringBoot对静态资源的映射规则1. 默认静态资源位置2. 自定义配置

一、springmvc 对静态资源的映射规则

<!-- 设置静态资源不过滤 --><mvc:resources location="/css/" mapping="/css/**" /><mvc:resources location="/images/" mapping="/images/**" /><mvc:resources location="/js/" mapping="/js/**" />

二、SpringBoot对静态资源的映射规则

1. 默认静态资源位置

1.1 所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;

注意:在访问的时候只需要写webjars下面资源的名称即可

webjars:以jar包的方式引入静态资源,在pom文件中引入依赖:

<!-- 引入jquery的webjar --><dependency><groupId>org.webjars</groupId><artifactId>jquery</artifactId><version>3.3.1</version></dependency>

1.2 “/**” 访问当前项目的任何资源,都去(静态资源的文件夹)找映射

先看其源码:

@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {if (!this.resourceProperties.isAddMappings()) {logger.debug("Default resource handling disabled");return;}Duration cachePeriod = this.resourceProperties.getCache().getPeriod();CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();if (!registry.hasMappingForPattern("/webjars/**")) {customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/").setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));}String staticPathPattern = this.mvcProperties.getStaticPathPattern();if (!registry.hasMappingForPattern(staticPathPattern)) {customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));}}

ResourceProperties.java

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)public class ResourceProperties {private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {"classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/" };

从源码可以得出访问路径:

"classpath:/META‐INF/resources/","classpath:/resources/","classpath:/static/","classpath:/public/""/":当前项目的根路径

前面4个是resource目录下的不同目录

注意:最后一个 / 就是表示 webapp 目录中的静态资源也不被拦截。这个不经常使用。

idea中创建好Spring Boot项目,默认的目录就有static

如果要访问static下面的hello.jpg ,在浏览器输入

**http://localhost:8080/hello.jpg ** 就可以去静态资源文件夹里面找hello.jpg

1.3 访问欢迎页; 静态资源文件夹下的所有index.html页面;被"/**"映射;

1.4 配置需要的图标,所有的 **/favicon.ico 都是在静态资源文件下找;

2. 自定义配置

第一种: 在Springboot中可以直接在配置文件中覆盖默认的静态资源路径的配置信息:

#配置定义的资源位置spring.resources.static-locations=classpath:/#配置定义的请求url规则spring.mvc.static-path-pattern=/**

第二种: 在SpringBoot开发中,可以在Java代码中覆盖默认静态资源配置

@Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {if(!registry.hasMappingForPattern("/static/**")){registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");}super.addResourceHandlers(registry);}}

由于水平有限,本博客难免有不足,恳请各位大佬不吝赐教!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。