一、静态资源导入
1.源码分析
查看源码:WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter中有关资源处理的方法addResourceHandlers如下:
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
解析:整个方法就是一个大的if-else的结构
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
}
这里if表示我们的配置文件中有添加设置,打印日志:禁用默认资源处理:
yaml中添加静态资源设置:
spring:
mvc:
static-path-pattern: XXX
else里面:
- 缓存控制
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
- 可以访问/webjars/**下的静态文件,
- Webjars本质就是以jar包的方式引入我们的静态资源 , 我们以前要导入一个静态资源文件,直接导入即可。可以访问网站:https://www.webjars.com/找的需要的jar包。
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
这里导入jQuery的jar包为例:
导入webjars下的jQuery
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
找到源代码中classpath:/META-INF/resources/webjars/路径
访问jquery.js:
- else里面的另一个if语句:
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
再查看getResourceLocations的源码,就可以看到可以被加载静态资源的路径:
{"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/" }
- web项目中resource目录下
- 项目中的resource/resource目录下
- 项目中的resource/static目录下
- 项目中的resource/public目录下
2.小结
静态资源加载:
- 自定义目录,其他配置目录将全部失效(resource/static,resource/public…)
spring:
mvc:
static-path-pattern: classpath:/code1/,classpath:/code2/
配置后有效的只有:resource/code1和resource/code2
- webjars静态加载(一般不用)
- 不自定义目录,可以放静态资源的目录,可以直接访问http://localhost:8080/XXX.js
- web项目中resource目录下
- 项目中的resource/resource目录下
- 项目中的resource/static目录下
- 项目中的resource/public目录下
- 优先级resource>static>public
二、设置首页
springboot中我们直接访问http://localhost:8080/的页面为:
我们如何来设置:
源码分析
WebMvcAutoConfiguration下:
private Optional<Resource> getWelcomePage() {
String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
private Resource getIndexHtml(String location) {
return this.resourceLoader.getResource(location + "index.html");
}
可以看出会加载静态资源中的index.html,之前是因为没有index.html,所有页面才会显示Error Page
我们再resource/public目录下新建一个index.html(resource/static等等)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>
再次访问http://localhost:8080/: