Spring Boot 学习(3)静态资源配置

写在前面:最近利用晚上的时间在网上看视频学习Spring Boot,这博客是对学习的一点点总结及记录,技术是开源的、知识是共享的
如果你对Spring Boot 感兴趣,可以关注我的动态,我们一起学习。用知识改变命运,让家人过上更好的生活

一、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.png 就可以去静态资源文件夹里面找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);
    }

}
发布了73 篇原创文章 · 获赞 795 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_43570367/article/details/103620882