1.静态资源处理
SpringBoot中,SpringMVC的web配置都在 WebMvcAutoConfiguration 这个配置类里面,而当程序识别出用户发出的URL请求的是静态资源而不是控制器的话,这时便会调用WebMvcAutoConfiguration类下的addResourceHandlers方法,该方法具体如下:
@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();
// webjars 配置
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));
}
}
addResourceHandlers方法中定义了两种不同的静态资源映射规则。
1.1 webjars
第一种静态资源映射规则是对于 “ /webjars/** ”的URL,程序会去classpath:/META-INF/resources/webjars/ 找对应的资源
1.2 /**
第二种静态资源映射规则是对于“ /** ”的URL,程序会分别去找以下四个目录下的文件,访问的优先级正好也是从高到低,即优先访问"classpath:/META-INF/resources/" 文件夹下的文件
“classpath:/META-INF/resources/”
“classpath:/resources/”
“classpath:/static/”
“classpath:/public/”
这点结合addResourceHandlers方法以及resourceProperties类中的下述代码便可得出结论
// 进入方法
public String[] getStaticLocations() {
return this.staticLocations;
}
// 找到对应的值
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
// 找到路径
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
};
此外,如果想要自定义静态资源路径,可以在配置文件中application.properties中进行配置,如
spring.resources.static-locations=classpath:/coding/,classpath:/xu/
那么,当程序就会分别去找“classpath:/coding/”以及“classpath:/xu/”目录下的静态资源文件。
2.首页处理
首页处理与静态资源处理的相关源码都在WebMvcAutoConfiguration这个配置类里面,在WebMvcAutoConfiguration配置类中,找到以下代码:
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
FormattingConversionService mvcConversionService,
ResourceUrlProvider mvcResourceUrlProvider) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(), // getWelcomePage 获得欢迎页
this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
return welcomePageHandlerMapping;
}
private Optional<Resource> getWelcomePage() {
String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
// ::是java8 中新引入的运算符
// Class::function的时候function是属于Class的,应该是静态方法。
// this::function的funtion是属于这个对象的。
// 简而言之,就是一种语法糖而已,是一种简写
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
// 欢迎页就是一个location下的的 index.html 而已
private Resource getIndexHtml(String location) {
return this.resourceLoader.getResource(location + "index.html");
}
上述代码主要的内容是,欢迎页面就是位于静态资源文件夹下面的index.html页面,比如我访问 http://localhost:8080/ ,程序就会找静态资源文件夹下的 index.html页面返回给用户。
例如在类路径下创建一个名为resources的文件夹,并在该文件夹中创建index.html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>
随后启动程序,访问 http://localhost:8080,便能看到下面的页面: