国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Springboot靜態資源訪問實現代碼解析

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

springboot靜態資源加載默認是從/static(或/public或/resources或/META-INF/resources) 目錄下加載靜態資源。

加載的優選級別:/META-INF/resources》/resources》/public》/static

靜態資源的加載源碼分析(WebMvcAutoConfiguration類)

首先從WebMvcAutoConfiguration.class自動配置類部分代碼來看:

//添加靜態資源規則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依賴映射規則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));}//本地配置的映射規則//this.resourceProperties.getStaticLocations() 從ResourceProperties中加載靜態路徑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;

映射規則總結

在springboot靜態資源加載規則中,除了”標準“靜態資源位置之外,還有一個較為特殊的WebJars

“標準”靜態資源映射規則

​所有的“/**”的請求,如果沒有對應的處理,那么就去默認映射的靜態資源目錄下去找,如下所示:

'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靜態資源訪問實現代碼解析

springboot默認歡迎頁面

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

自定義配置靜態資源目錄

在application.properties文件中去配置

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

遇到的坑

在配置了靜態資源目錄的時候,跳轉到的頁面路徑不能寫絕對路徑,

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

Springboot靜態資源訪問實現代碼解析

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

Springboot靜態資源訪問實現代碼解析

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

Springboot靜態資源訪問實現代碼解析

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 成年人视频免费网站 | 黄色欧美网站 | 国产乱码精品一区二区三区四川人 | 国产高清一级视频在线观看 | 中文三级视频 | 欧美大尺度aaa级毛片 | 国产精品揄拍一区二区 | 久久久久亚洲精品中文字幕 | 亚洲精品系列 | 男女上下爽无遮挡午夜免费视频 | 国产极品精频在线观看 | 欧洲欧美成人免费大片 | 国产精品毛片 | 欧美日本一区二区 | 曰本aaaaa毛片午夜网站 | 成人免费观看www在线 | 欧美一区不卡二区不卡三区 | 精品久久在线 | 啪视| 成人在线网站 | 91精品成人免费国产 | 欧美一级性视频 | 国产不卡毛片 | 亚洲 欧美 日韩中文字幕一区二区 | 日本韩国台湾香港三级 | 人成在线免费视频 | 亚洲综合一 | 日本免费特黄aa毛片 | 国产三级在线视频播放线 | 色综合九九 | a成人在线 | 久草免费福利 | 色综合亚洲七七久久桃花影院 | 国产亚洲片 | 精品国产三级a∨在线 | 成人精品国产亚洲欧洲 | 国产盗摄精品一区二区三区 | 国产下药迷倒白嫩丰满美女j8 | 最新国产三级在线不卡视频 | 免费五级在线观看日本片 | 黄大片日本一级在线a |