1.1 WebMvc自动配置
- org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
// 添加资源映射
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
// 从ResourceProperties中获取缓存时间
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
// 详见 1.2 webjars,请求为/webjars/**
this.customizeResourceHandlerRegistration(registry.
// /webjars/**的请求
addResourceHandler(new String[]{"/webjars/**"}).
// 都在 classpath:/META-INF/resources/webjars/ 中获取资源
addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).
// 设置缓存时间
setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
// staticPathPattern = "/**";
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
// 详见 1.3 请求为 /**
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.
// /**请求
addResourceHandler(new String[]{staticPathPattern}).
// 都在"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"中获取资源
addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).
setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
- ResourceProperties
// 设置和静态资源有关的参数
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
private String[] staticLocations;
private boolean addMappings;
private final ResourceProperties.Chain chain;
private final ResourceProperties.Cache cache;
1.2 webjars,请求为/webjars/**
-
webjars:以jar包的方式引入静态资源
-
引入jq的jar包
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1-1</version>
</dependency>
1.2.1 测试
- 访问http://localhost:8080/webjars/jquery/3.3.1-1/jquery.js
- 所有 /webjars/**的请求 ,都在 classpath:/META-INF/resources/webjars/ 中获取资源
1.3 请求为 /**
1.3.1 静态资源路径
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
- 访问localhost:8080/xxx
如果该请求项目未处理,就到静态资源路径获取xxx资源
1.4 欢迎页
- org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
// getStaticPathPattern : 静态资源映射(/**)
return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
}
- 静态资源文件夹下的所有index.html页面;被"/**"映射
1.4.1 测试
访问:http://localhost:8080
结果:404
1.4.2 添加首页
在resource中添加public或static,添加index.html,再次访问
- index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
看什么?6;你和我,走一波!
</body>
</html>
1.5 自定义图标
- org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
// 使用spring.mvc.favicon.enabled启用自定义图标
// matchIfMissing : 不配置也默认启用
@Configuration
@ConditionalOnProperty(
value = {"spring.mvc.favicon.enabled"},
matchIfMissing = true
)
public static class FaviconConfiguration implements ResourceLoaderAware {
private final ResourceProperties resourceProperties;
private ResourceLoader resourceLoader;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(-2147483647);
// 所有的 **/favicon.ico请求都映射到faviconRequestHandler
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler.setLocations(this.resolveFaviconLocations());
return requestHandler;
}
private List<Resource> resolveFaviconLocations() {
String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
List<Resource> locations = new ArrayList(staticLocations.length + 1);
Stream var10000 = Arrays.stream(staticLocations);
ResourceLoader var10001 = this.resourceLoader;
this.resourceLoader.getClass();
var10000.map(var10001::getResource).forEach(locations::add);
locations.add(new ClassPathResource("/"));
return Collections.unmodifiableList(locations);
}
}
1.5.1 结论
所有的 **/favicon.ico 都是在静态资源文件下找
1.6 自定义静态资源文件夹
- application.properties
# 自定义静态资源文件夹
spring.resources.static-locations=classpath:/static,classpath:/public