SpringBoot对静态资源的映射规则
- 这段源码中配置了默认的静态资源路径
@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/" };
- 这段代码配置了外部引入的静态资源文件结构
@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));
}
}
webjars网站
https://www.webjars.org/
引入jQuery依赖
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
这个目录和源码的映射路径一致
http://localhost:8090/webjars/jquery/3.5.1/jquery.js
在浏览器地址栏输入以上网站就可以访问
- "/**"的写法:访问当前项目的任何资源
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
classpath 就是下图中的resource,在给这个目录下建META-INF/resources等都是可以访问到的
localhost:8080/abc === 去静态资源下找abc
- 欢迎页:静态资源文件夹下所有index.html页面;被“/**”映射
localhost:8080/ 找index页面
- 配置静态资源位置
spring.resources.static-locations=classpath:/suitianshuang/