本文共 7934 字,大约阅读时间需要 26 分钟。
在web开发中,静态资源的访问是必不可少的,如:图片、js、css 等资源的访问。spring Boot 对静态资源访问提供了很好的支持,基本使用默认配置就能满足开发需求。
SpringBoot默认为我们提供了静态资源处理,使用WebMvcAutoConfiguration 中的配置各种属性。(注意:如果想自己完全控制webmvc 则可以在自己定义的@Configuration配置类中增加@EnableWebMvc,使默认的WebMvcAutoConfiguration失效)查看WebMvcAutoConfiguration源码:
// Defined as a nested config to ensure WebMvcConfigurer is not read when not // on the classpath @Configuration @Import(EnableWebMvcConfiguration.class) @EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class }) public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware { private static final Log logger = LogFactory.getLog(WebMvcConfigurer.class); /** *静态资源位置定义:CLASSPATH_RESOURCE_LOCATIONS属性定义了默认的位置 * "classpath:/META-INF/resources/", * "classpath:/resources/", * "classpath:/static/", * "classpath:/public/" **/ //。。。。。。。。。。。省略代码。。。。。。。。。。。。。。。。。 //默认静态资源实现核心代码,如果需要自己处理静态资源可以参考改代码 @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)); } } //。。。。。。。。。。。省略代码。。。。。。。。。。。。。。。。。 }
通过代码分析默认配置/**的静态资源映射路径为:
"classpath:/META-INF/resources/", "classpath:/resources/","classpath:/static/", "classpath:/public/"
通过debug发现最终的映射路径信息如下图所示:
根据以上图片得知,所有的映射路径最终都会加上”/”(注意:在使用配置文件时才会自动加上,或者覆写方法时参考底层实现),最终效果如下:"classpath:/META-INF/resources/", "classpath:/resources/","classpath:/static/", "classpath:/public/","/"
默认配置的 /webjars/** 映射到 classpath:/META-INF/resources/webjars/
注意:上面的 static、public、resources 等目录都在 classpath: 下面(如 src/main/resources/static)。资源映射的优先级顺序为:META-INF/resources > resources > static > public(可以放入同一个文件进行测试)。如果项目中没有对应的映射路径可以手动创建。
默认映射路径效果图如下:
分别在对应的映射路径中增加文件1.png,然后启动服务访问如下路径查看情况:
http://127.0.0.1:8080/1.png
在static下面增加文件index.html,内容如下:
Insert title here 映射目录测试!
默认情况下,SpringBoot从classpath下的/static(/public,/resources或/META-INF/resources)文件夹,或从ServletContext根目录提供静态内容。这是通过SpringMVC的ResourceHttpRequestHandler实现的,我们可以通过实现接口WebMvcConfigurer(注意在jdk1.8之前的版本中需要继承类WebMvcConfigurerAdapter)并实现addResourceHandlers方法来改变该行为(加载静态文件)或者通过修改配置文件来完成目录的映射。
在src/main/resources/下创建一个文件夹view,并且创建文件index_view.html,使用以下方式使该文件能够正常访问。访问方式:
http://127.0.0.1:8080/index_view.html
package com.sunld;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/** * 自定义资源映射路径处理类 * @author sunliaodong */@Configurationpublic class MyWebMvcConfigurer implements WebMvcConfigurer{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //将/** 目录下的静态资源映射到classpath:/view/下 registry.addResourceHandler("/**").addResourceLocations("classpath:/view/"); }}
访问:
http://127.0.0.1:8080/index_view.html
可以正常访问,但是图片信息,获取不到,修改代码如下即可:
package com.sunld;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/** * 自定义资源映射路径处理类 * @author sunliaodong */@Configuration//增加该注解之后WebMvcAutoConfiguration中配置就不会生效//@EnableWebMvcpublic class MyWebMvcConfigurer implements WebMvcConfigurer{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { /** * 将/** 目录下的静态资源映射到classpath:/view/下 * 直接访问http://127.0.0.1:8080/index_view.html,发现图片信息获取不到需要增加默认的资源映射 * Locations of static resources. Defaults to classpath:[/META-INF/resources/, * /resources/, /static/, /public/]. */ // registry.addResourceHandler("/**").addResourceLocations( "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "classpath:/view/"); }}
首先删除类MyWebMvcConfigurer。
在application.properties下添加如下信息:#修改默认资源路径映射# 默认值为 /**spring.mvc.static-path-pattern=/**# 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # 这里设置要指向的路径,多个使用英文逗号隔开,不影响默认的静态资源映射spring.resources.static-locations=classpath:/view/
发现配置的view下的页面可以正常访问,以及/META-INF/resources/下的图片可以正常访问,其他默认的静态资源映射路径无法正常访问。按照以下方式修改配置文件即可
#修改默认资源路径映射# 默认值为 /**spring.mvc.static-path-pattern=/**# 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # 这里设置要指向的路径,多个使用英文逗号隔开,不影响默认的静态资源映射spring.resources.static-locations=classpath:/view/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
把view2/** 映射到classpath:/view/
访问方式:http://127.0.0.1:8080/view2/index_view2.html
配置中配置了静态模式为/view2/,就只能通过/view2/来访问。
package com.sunld;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/** * 自定义资源映射路径处理类 * @author sunliaodong */@Configuration//增加该注解之后WebMvcAutoConfiguration中配置就不会生效//@EnableWebMvcpublic class MyWebMvcConfigurer implements WebMvcConfigurer{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { /** * 将/** 目录下的静态资源映射到classpath:/view/下 * 直接访问http://127.0.0.1:8080/index_view.html,发现图片信息获取不到需要增加默认的资源映射 * Locations of static resources. Defaults to classpath:[/META-INF/resources/, * /resources/, /static/, /public/]. */ registry.addResourceHandler("/**").addResourceLocations( "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "classpath:/view/"); /** * 将/view2/** 目录映射到classpath:/view2下 * 这种方式不影响springboot默认的静态资源映射路径,可以注释掉以上代码测试 */ registry.addResourceHandler("/view2/**").addResourceLocations( "classpath:/view2/"); }}
首先删除类:MyWebMvcConfigurer然后修改配置文件
#增加新的资源路径映射spring.mvc.static-path-pattern=/view2/**spring.resources.static-locations=classpath:/view2/
发现默认的静态资源信息访问不到。
修改默认的资源路径映射:
添加新的资源路径映射: