SpringBoot靜態(tài)資源與首頁配置實(shí)現(xiàn)原理深入分析
一、靜態(tài)資源導(dǎo)入
關(guān)鍵源碼可以看WebMvcAutoConfiguration這個類下面的addResourceHandlers方法

在這個方法中,我們有幾個重點(diǎn)需要了解一下
1、webjars
可以理解為以maven的形式引入web的相關(guān)jar包
請求路徑為/webjars/**的,都會去classpath:/META-INF/resources/webjars/下尋找相關(guān)的靜態(tài)資源
2、靜態(tài)資源映射規(guī)則
如果在項(xiàng)目中要使用我們自己導(dǎo)入的靜態(tài)資源,它的映射規(guī)則是怎么樣的呢,我們分析源碼可以得出

以下四個路徑的中存放的靜態(tài)資源可以被識別,優(yōu)先
resource(注意,此處是resource下面的resource文件夾)>static (默認(rèn))>public
"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"
3、自定義靜態(tài)資源路徑
我們可以使用spring.web.resources.static-locations
在yaml文件中自定義靜態(tài)資源文件的路徑,例如我們限制靜態(tài)文件都必須放在static目錄下
也可以使用spring.mvc.static-path-pattern,當(dāng)前項(xiàng)目 + static-path-pattern + 靜態(tài)資源名 = 靜態(tài)資源文件夾下找
spring:
web:
resources:
static-locations: classpath:/static/
mvc:
static-path-pattern: /static/**

隨后我們訪問一下靜態(tài)資源,發(fā)現(xiàn)只有放在static下面可以被訪問到


二、首頁配置和圖標(biāo)
1、首頁配置
springboot它會去找靜態(tài)資源文件夾下的index.html(注意不能配置spring.mvc.static-path-pattern)或者是controller處理/index轉(zhuǎn)發(fā)的頁面
下面是WebMvcAutoConfiguration這個類中關(guān)于首頁的相關(guān)方法
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
return welcomePageHandlerMapping;
}
private Resource getWelcomePage() {
String[] var1 = this.resourceProperties.getStaticLocations();
int var2 = var1.length;
for(int var3 = 0; var3 < var2; ++var3) {
String location = var1[var3];
Resource indexHtml = this.getIndexHtml(location);
if (indexHtml != null) {
return indexHtml;
}
}
ServletContext servletContext = this.getServletContext();
if (servletContext != null) {
return this.getIndexHtml((Resource)(new ServletContextResource(servletContext, "/")));
} else {
return null;
}
}
private Resource getIndexHtml(String location) {
return this.getIndexHtml(this.resourceLoader.getResource(location));
}
private Resource getIndexHtml(Resource location) {
try {
Resource resource = location.createRelative("index.html");
if (resource.exists() && resource.getURL() != null) {
return resource;
}
} catch (Exception var3) {
}
return null;
}2、圖標(biāo)
官網(wǎng)是說在靜態(tài)資源路徑下放置一個favicon.ico,spring boot就會自動識別

如圖

圖標(biāo)加載成功 可能會因?yàn)榫彺婕虞d不出來 清除緩存多試幾次就行了

到此這篇關(guān)于SpringBoot靜態(tài)資源與首頁配置實(shí)現(xiàn)原理深入分析的文章就介紹到這了,更多相關(guān)SpringBoot靜態(tài)資源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中使用MongoDB數(shù)據(jù)庫實(shí)例Demo
MongoDB是由C++語言編寫的,基于分布式文件存儲的數(shù)據(jù)庫,是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是最接近于關(guān)系型數(shù)據(jù)庫的NoSQL數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于Java中使用MongoDB數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下2023-12-12
SpringBoot集成Milvus和deeplearning4j實(shí)現(xiàn)圖搜圖功能
Milvus?是一種高性能、高擴(kuò)展性的向量數(shù)據(jù)庫,可在從筆記本電腦到大型分布式系統(tǒng)等各種環(huán)境中高效運(yùn)行,Deeplearning4j(DL4J)是一個開源的深度學(xué)習(xí)框架,專門為Java和Scala開發(fā),本文給大家介紹了SpringBoot集成Milvus和deeplearning4j實(shí)現(xiàn)圖搜圖功能2024-10-10
javaweb中ajax請求后臺servlet(實(shí)例)
下面小編就為大家?guī)硪黄猨avaweb中ajax請求后臺servlet(實(shí)例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
SpringCloud環(huán)境搭建過程之Rest使用小結(jié)
這篇文章主要介紹了SpringCloud環(huán)境搭建之Rest使用,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
Java數(shù)據(jù)類型之引用數(shù)據(jù)類型解讀
這篇文章主要介紹了Java數(shù)據(jù)類型之引用數(shù)據(jù)類型,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

