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

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

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

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

相关文章:

Springboot 系列文章


一、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 “/**” 访问当前项目的任何资源,都去(静态资源的文件夹)找映射

先看其源码:

		@Override
		public 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代码中覆盖默认静态资源配置

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        if(!registry.hasMappingForPattern("/static/**")){
            registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        }
        super.addResourceHandlers(registry);
    }

}

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

评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值