Springboot靜態(tài)資源訪問實(shí)現(xiàn)代碼解析
springboot靜態(tài)資源加載默認(rèn)是從/static(或/public或/resources或/META-INF/resources) 目錄下加載靜態(tài)資源。
加載的優(yōu)選級別:/META-INF/resources》/resources》/public》/static
靜態(tài)資源的加載源碼分析(WebMvcAutoConfiguration類)
首先從WebMvcAutoConfiguration.class自動配置類部分代碼來看:
//添加靜態(tài)資源規(guī)則
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
//webjars依賴映射規(guī)則
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
//本地配置的映射規(guī)則
//this.resourceProperties.getStaticLocations() 從ResourceProperties中加載靜態(tài)路徑
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
ResourceProperties類部分源碼
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties {
//springboot默認(rèn)的加載路徑
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;
映射規(guī)則總結(jié)
在springboot靜態(tài)資源加載規(guī)則中,除了”標(biāo)準(zhǔn)“靜態(tài)資源位置之外,還有一個(gè)較為特殊的WebJars
“標(biāo)準(zhǔn)”靜態(tài)資源映射規(guī)則
所有的“/**”的請求,如果沒有對應(yīng)的處理,那么就去默認(rèn)映射的靜態(tài)資源目錄下去找,如下所示:
- "classpath:/META-INF/resources/"
- "classpath:/resources/"
- "classpath:/static/",
- "classpath:/public/"
- “/**”
所有的webjars的請求都會去 ”classpath:/META-INF/resources/webjars/**“去資源
(如果 以jar包的方式來引入jquery包)
在pom.xml中引入依賴
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1-2</version>
</dependency>
從引入的包目錄來看

springboot默認(rèn)歡迎頁面
自動去加載默認(rèn)目錄下的index.html;如static/index.html
自定義配置靜態(tài)資源目錄
在application.properties文件中去配置
//配置test為靜態(tài)資源目錄
spring.resources.static-locations=classpath:/test/
遇到的坑
在配置了靜態(tài)資源目錄的時(shí)候,跳轉(zhuǎn)到的頁面路徑不能寫絕對路徑,
比如:spring.resources.static-locations=classpath:/test/ 我配置test為靜態(tài)資源的加載位置,在訪問的時(shí)候不需要寫test

請求:http://127.0.0.1:8085/test/1.png

請求:http://127.0.0.1:8085/1.png

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java面試題沖刺第十三天--數(shù)據(jù)庫(3)
這篇文章主要為大家分享了最有價(jià)值的三道數(shù)據(jù)庫面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-07-07
springboot優(yōu)雅獲取前端參數(shù)的方法詳解
現(xiàn)在的項(xiàng)目基本上都是前后端分離的項(xiàng)目,如何打通前后端,接收前端傳過來的參數(shù)呢,這篇文章小編就來和大家詳細(xì)介紹一下springboot如何優(yōu)雅的獲取前端參數(shù)吧2024-03-03
Mybatis插入Oracle數(shù)據(jù)庫日期型數(shù)據(jù)過程解析
這篇文章主要介紹了Mybatis插入Oracle數(shù)據(jù)庫日期型數(shù)據(jù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java+MySQL實(shí)現(xiàn)圖書管理系統(tǒng)(完整代碼)
這篇文章主要介紹了Java+MySQL實(shí)現(xiàn)圖書管理系統(tǒng)(完整代碼),本文給大家介紹的非常想詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Eclipse+Java+Swing實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)的實(shí)例代碼
這篇文章主要介紹了Eclipse+Java+Swing實(shí)現(xiàn)學(xué)生成績管理系統(tǒng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Java toString方法重寫工具之ToStringBuilder案例詳解
這篇文章主要介紹了Java toString方法重寫工具之ToStringBuilder案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

