av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

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

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類)

首先從WebMvcAutoConfiguration.class自動(dòng)配置類部分代碼來(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依賴映射規(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ī)則

​所有的“/**”的請(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中引入依賴

<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1-2</version> </dependency>

從引入的包目錄來(lái)看

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

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è)面路徑不能寫絕對(duì)路徑,

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

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

請(qǐng)求:http://127.0.0.1:8085/test/1.png

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

請(qǐng)求:http://127.0.0.1:8085/1.png

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美日韩国产三级 | 日本不卡高字幕在线2019 | 日韩精品免费在线观看 | 少妇高潮久久久久久潘金莲 | 欧美在线视频一区 | 狠狠涩 | 精品久久影院 | 日日操夜夜干 | 在线播放一区 | 在线免费看av| 免费网站黄 | 亚洲一区二区欧美 | 中文字幕国产在线 | 国产精品一区二区不卡 | 国产亚洲视频在线观看 | 大尺度性做爰的小说金陵春 | 人人综合 | 欧美亚洲国产精品 | 日本在线免费观看视频 | 欧美 日韩 国产 成人 在线 | 精品理论片 | 性生活毛片 | 久久国产精品视频 | 2025国产精品 | 日韩亚洲欧美在线 | 久久综合热 | 日韩一区二区不卡 | 精品免费 | 欧美大白屁股 | 手机成人在线视频 | 色综合一区二区 | av片免费观看| 国产欧美一区二区三区在线看蜜臀 | 欧美激情综合 | 日日爽天天 | 欧美视频区 | 免费的毛片 | 欧美一级视频在线观看 | 久久久亚洲一区 | av不卡在线播放 | 欧美黄色一区二区 |