Springboot靜態(tài)資源訪問(wèn)實(shí)現(xiàn)代碼解析
springboot靜態(tài)資源加載默認(rèn)是從/static(或/public或/resources或/META-INF/resources) 目錄下加載靜態(tài)資源。
加載的優(yōu)選級(jí)別:/META-INF/resources》/resources》/public》/static
靜態(tài)資源的加載源碼分析(WebMvcAutoConfiguration類(lèi))
首先從WebMvcAutoConfiguration.class自動(dòng)配置類(lèi)部分代碼來(lái)看:
//添加靜態(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依賴(lài)映射規(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類(lèi)部分源碼
@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ī)則
所有的“/**”的請(qǐng)求,如果沒(méi)有對(duì)應(yīng)的處理,那么就去默認(rèn)映射的靜態(tài)資源目錄下去找,如下所示:
- "classpath:/META-INF/resources/"
- "classpath:/resources/"
- "classpath:/static/",
- "classpath:/public/"
- “/**”
所有的webjars的請(qǐng)求都會(huì)去 ”classpath:/META-INF/resources/webjars/**“去資源
(如果 以jar包的方式來(lái)引入jquery包)
在pom.xml中引入依賴(lài)
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1-2</version> </dependency>
從引入的包目錄來(lái)看
springboot默認(rèn)歡迎頁(yè)面
自動(dòng)去加載默認(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)到的頁(yè)面路徑不能寫(xiě)絕對(duì)路徑,
比如:spring.resources.static-locations=classpath:/test/ 我配置test為靜態(tài)資源的加載位置,在訪問(wèn)的時(shí)候不需要寫(xiě)test
請(qǐng)求:http://127.0.0.1:8085/test/1.png
請(qǐng)求:http://127.0.0.1:8085/1.png
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot應(yīng)用中靜態(tài)資源訪問(wèn)與接口請(qǐng)求沖突問(wèn)題解決
- SpringBoot深入探究四種靜態(tài)資源訪問(wèn)的方式
- SpringBoot設(shè)置靜態(tài)資源訪問(wèn)控制和封裝集成方案
- SpringBoot中的yaml語(yǔ)法及靜態(tài)資源訪問(wèn)問(wèn)題
- SpringBoot中的靜態(tài)資源訪問(wèn)的實(shí)現(xiàn)
- 在SpringBoot中靜態(tài)資源訪問(wèn)方法
- SpringBoot中的static靜態(tài)資源訪問(wèn)、參數(shù)配置、代碼自定義訪問(wèn)規(guī)則詳解
相關(guān)文章
SpringBoot如何使用feign實(shí)現(xiàn)遠(yuǎn)程接口調(diào)用和錯(cuò)誤熔斷
這篇文章主要介紹了SpringBoot如何使用feign實(shí)現(xiàn)遠(yuǎn)程接口調(diào)用和錯(cuò)誤熔斷,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Spring Security使用單點(diǎn)登錄的權(quán)限功能
本文主要介紹了Spring Security使用單點(diǎn)登錄的權(quán)限功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04mybatis if test判斷BigDecimal遇到的坑及解決
這篇文章主要介紹了mybatis if test判斷BigDecimal遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03實(shí)踐講解SpringBoot自定義初始化Bean+HashMap優(yōu)化策略模式
本篇講解了SpringBoot自定義初始化Bean+HashMap優(yōu)化策略模式,通過(guò)實(shí)踐的方式更通俗易懂,對(duì)此不了解的同學(xué)跟著小編往下看吧2021-09-09AJAX?SpringBoot?前后端數(shù)據(jù)交互的項(xiàng)目實(shí)現(xiàn)
本文主要介紹了AJAX?SpringBoot?前后端數(shù)據(jù)交互的項(xiàng)目實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03java 實(shí)現(xiàn)最小二叉樹(shù)堆排序的實(shí)例
這篇文章主要介紹了java 實(shí)現(xiàn)最小二叉樹(shù)堆排序的實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09如何用Java Stream寫(xiě)出既高雅又裝*的代碼
如何讓同事看不懂你寫(xiě)的代碼,然后覺(jué)得你非常牛逼,這里用到了stream()與Lambda,需要有點(diǎn)基礎(chǔ),沒(méi)基礎(chǔ)你炫個(gè)🔨優(yōu)雅永不過(guò)時(shí)~ 看下面文章時(shí)記得穿燕尾服,拿高腳杯2021-08-08