亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Springboot靜態(tài)資源訪問實現(xiàn)代碼解析

瀏覽:5日期:2023-05-14 08:27:21

springboot靜態(tài)資源加載默認是從/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默認的加載路徑 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ī)則中,除了”標準“靜態(tài)資源位置之外,還有一個較為特殊的WebJars

“標準”靜態(tài)資源映射規(guī)則

​所有的“/**”的請求,如果沒有對應(yīng)的處理,那么就去默認映射的靜態(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靜態(tài)資源訪問實現(xiàn)代碼解析

springboot默認歡迎頁面

自動去加載默認目錄下的index.html;如static/index.html

自定義配置靜態(tài)資源目錄

在application.properties文件中去配置

//配置test為靜態(tài)資源目錄spring.resources.static-locations=classpath:/test/

遇到的坑

在配置了靜態(tài)資源目錄的時候,跳轉(zhuǎn)到的頁面路徑不能寫絕對路徑,

比如:spring.resources.static-locations=classpath:/test/ 我配置test為靜態(tài)資源的加載位置,在訪問的時候不需要寫test

Springboot靜態(tài)資源訪問實現(xiàn)代碼解析

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

Springboot靜態(tài)資源訪問實現(xiàn)代碼解析

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

Springboot靜態(tài)資源訪問實現(xiàn)代碼解析

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 中文字幕精品视频在线观看 | 日韩女性性生生活视频 | 亚洲精品一二三四区 | 国产素人自拍 | 国产在线一区二区三区欧美 | 国产中文字幕亚洲 | 日韩不卡| 色国产在线视频一区 | 美女动作一级毛片 | 激情性爽三级成人 | 欧美成人一级视频 | 欧美特级毛片a够爽 | 亚洲视频免 | 中国美女一级看片 | 日韩精品久久久毛片一区二区 | 国产免费一区二区三区免费视频 | 黄色免费一级 | 国产黄在线观看免费观看软件视频 | 亚洲午夜一级毛片 | 国产精品亚洲专区一区 | 嗯啊在线观看免费影院 | 精品国产成人a在线观看 | www.亚洲一区二区三区 | 精品欧美激情在线看 | 久久视频精品a线视频在线观看 | 亚洲欧美日韩中文字幕网址 | 福利视频在线观看视频 | 国产精品视频分类一区 | 亚洲一级黄色毛片 | 狠狠的日视频 | 一区二区不卡在线 | 久久综合精品国产一区二区三区 | www日韩免费高清视频 | 久操香蕉 | 国产在线播放网址 | 麻豆国产视频 | 亚洲国产精品日韩在线 | 国产精品久久一区二区三区 | 欧美成人黄色片 | 亚洲欧美一区二区久久香蕉 | 免费看大黄高清网站视频在线 |