靜態資源映射處理以及靜態資源路徑的作用
一、靜態資源映射配置
1. 靜態資源映射在SpringBoot中的自動配置
WebMvcAutoConfiguration:
// 增加資源處理器---實際上就是增加資源路徑的映射關系,比如訪問地址/,將會映射到服務器的哪個文件夾下
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 當spring.resources.add-mappings為false時(默認為true),直接日志輸出以及結束函數,不會將下面的靜態資源路徑加入到注冊器中
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
// 反之spring.resources.add-mappings為true
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
// 如果注冊器中不存在/webjars/**這個路徑的映射,則將這個classpath:/META-INF/resources/webjars/資源路徑映射到/webjars/**訪問路徑,webjars這個路徑用於存放將靜態資源打包成jar包后的資源,這里不做具體說明(不常用)
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
// 獲取mvc配置類中的靜態路徑(默認為/**)
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
// 如果這個路徑在注冊器中沒有映射
if (!registry.hasMappingForPattern(staticPathPattern)) {
// 將resource配置類中的靜態資源路徑(默認為"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/")映射到mvc配置類中的靜態路徑(默認為/**),這里相當於當我們訪問 / 路徑下的文件時,會將其映射到前面的靜態資源路徑
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern) .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
// 經過上面配置類的加載(自動配置)后,我們可以將靜態資源放在以下的路徑下供於靜態資源的訪問。
// addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
這些路徑默認會被映射到/**(registry.addResourceHandler(staticPathPattern))
比如訪問 http://localhost:8080/1.js 將會訪問上面這些路徑下的1.js文件
優先級以此遞減(如果/META-INF/resources/中有對應文件,就不會去resources、static、public目錄下找文件)
或者通過加入webjar的依賴包再訪問如http://localhost:8080/webjars/jquery/3.4.1/jquery.js這個路徑進行靜態資源的訪問(不常用)
2.使用自定義的映射
注意:使用自定義映射之后,自動配置的靜態資源映射將失效(因為會進行覆蓋)
spring:
mvc:
static-path-pattern: /**
resources:
static-locations: classpath:/hello/, classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/
# 這里實際上就是修改了自動配置中的這部分參數customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
二、歡迎頁映射---基於靜態資源映射的歡迎頁
WebMvcAutoConfiguration:
// 歡迎頁處理映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
// 實例化歡迎頁處理映射,在這里指定了訪問路徑(this.mvcProperties.getStaticPathPattern()---前面配置好的mvc配置類中的靜態路徑/**)以及資源路徑(getWelcomePage()---實際上就是前面配置好的靜態資源路徑下的index.html這個文件)
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
return welcomePageHandlerMapping;
}
// 歡迎頁處理映射構造函數
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,
ApplicationContext applicationContext, Optional<Resource> welcomePage, String staticPathPattern) {
// 注意這里只有當staticPathPattern=/**時才會加載歡迎頁
if (welcomePage.isPresent() && "/**".equals(staticPathPattern)) {
logger.info("Adding welcome page: " + welcomePage.get());
setRootViewName("forward:index.html");
}
else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
logger.info("Adding welcome page template: index");
setRootViewName("index");
}
}
當我們訪問靜態資源路徑時(http://localhost:8080/)時,將會在靜態資源路徑下面尋找index.html文件,並將其作為歡迎頁(首頁)返回,注意這里不能修改spring.mvc.static-path-pattern,否則將無法自動配置歡迎頁