SpringBoot對(duì)靜態(tài)資源的映射規(guī)則詳解解讀
Spring Boot對(duì)靜態(tài)資源的映射規(guī)則
1.所有/webjars/**,都去classpath:/META-INF/resources/webjars/找資源
webjars:以jar包方式引入靜態(tài)資源。
<!‐‐引入jquery‐webjar,在訪問的時(shí)候只需要寫webjars下面資源的名稱即可‐‐>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>

- 訪問路徑://127.0.0.1:8089/webjars/jquery/3.4.1/jquery.js
2. "/**" 訪問當(dāng)前項(xiàng)目的任何資源,都去(靜態(tài)資源的文件夾)找映射
"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":當(dāng)前項(xiàng)目的根路徑
localhost:8080/abc === 去靜態(tài)資源文件夾里面找abc
3. 歡迎頁:靜態(tài)資源文件夾下的所有的index.html頁面;被 /** 映射
localhost:8089/:找首頁(index.html)
4. 所有的 **/favicon.ico都是在靜態(tài)資源文件下找的。
@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();
//靜態(tài)資源文件夾映射
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
配置歡迎頁映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(getInterceptors());
return welcomePageHandlerMapping;
}@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;
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
// 映射**/favicon.ico
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler.setLocations(resolveFaviconLocations());
return requestHandler;
}
private List<Resource> resolveFaviconLocations() {
String[] staticLocations = getResourceLocations(this.resourceProperties.getStaticLocations());
List<Resource> locations = new ArrayList<>(staticLocations.length + 1);
Arrays.stream(staticLocations).map(this.resourceLoader::getResource).forEach(locations::add);
locations.add(new ClassPathResource("/"));
return Collections.unmodifiableList(locations);
}
}
}到此這篇關(guān)于SpringBoot對(duì)靜態(tài)資源的映射規(guī)則詳解解讀的文章就介紹到這了,更多相關(guān)SpringBoot靜態(tài)資源的映射規(guī)則內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot 核心模塊詳解(12 個(gè)模塊詳解及作用說明)
和 Spring 框架一樣,Spring Boot 框架也是由許多核心模塊組成的,每個(gè)模塊負(fù)責(zé)不同的功能點(diǎn),本文講著重于介紹 Spring Boot 相關(guān)的 12 個(gè)模塊的作用和功能,感興趣的朋友一起看看吧2025-04-04
Spring的實(shí)例工廠方法和靜態(tài)工廠方法實(shí)例代碼
這篇文章主要介紹了Spring的實(shí)例工廠方法和靜態(tài)工廠方法實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
基于SpringBoot+jQuery實(shí)現(xiàn)留言板功能
本文詳細(xì)介紹了基于SpringBoot和jQuery實(shí)現(xiàn)留言板功能的全過程,包括需求分析、接口定義、服務(wù)器端代碼實(shí)現(xiàn)、前端頁面代碼修改以及運(yùn)行測試,需要的朋友可以參考下2025-12-12
SpringBoot指標(biāo)監(jiān)控的實(shí)現(xiàn)
本文主要介紹了SpringBoot指標(biāo)監(jiān)控的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
idea創(chuàng)建springboot項(xiàng)目,Application.java不能運(yùn)行問題及解決
這篇文章主要介紹了idea創(chuàng)建springboot項(xiàng)目,Application.java不能運(yùn)行問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot集成Milvus和deeplearning4j實(shí)現(xiàn)圖搜圖功能
Milvus?是一種高性能、高擴(kuò)展性的向量數(shù)據(jù)庫,可在從筆記本電腦到大型分布式系統(tǒng)等各種環(huán)境中高效運(yùn)行,Deeplearning4j(DL4J)是一個(gè)開源的深度學(xué)習(xí)框架,專門為Java和Scala開發(fā),本文給大家介紹了SpringBoot集成Milvus和deeplearning4j實(shí)現(xiàn)圖搜圖功能2024-10-10
Java中String字符串轉(zhuǎn)具體對(duì)象的幾種常用方式
String對(duì)象可以用來存儲(chǔ)任何字符串類型的數(shù)據(jù),包括HTML、XML等格式的字符串,下面這篇文章主要給大家介紹了關(guān)于JavaString字符串轉(zhuǎn)具體對(duì)象的幾種常用方式,需要的朋友可以參考下2024-03-03

