這篇“SpringBoot靜態資源映射的方法”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“SpringBoot靜態資源映射的方法”文章吧。
成都創新互聯專注于建始企業網站建設,響應式網站設計,商城建設。建始網站建設公司,為建始等地區提供建站服務。全流程按需搭建網站,專業設計,全程項目跟蹤,成都創新互聯專業和態度為您提供的服務
SpringBoot對于SpringMVC的自動化配置都在WebMVCAutoConfiguration類中。
其中一個靜態內部類WebMvcAutoConfigurationAdapter實現了WebMvcConfigurer接口。(361)
WebMvcConfigurer接口中定義了addResourceHandlers處理靜態資源的默認映射關系.(500)
addResourceHandlers在WebMvcAutoConfigurationAdapter類中實現
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(); 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)); } 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)); } } }
其中
this.resourceProperties.getStaticLocations()
返回靜態資源的默認映射關系,
getStaticLocations()方法在ResourceProperties中定義
其中,
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
第五個默認的資源映射:在靜態方法getResourceLocations中定義
/
小結:
默認情況下,可以在以下五個位置放置靜態資源
classpath:/META-INF/resources/ classpath:/resources/ classpath:/static/ classpath:/public/ /
【靜態資源一般放在classpath:/static/目錄下】
favicon.ico是瀏覽器左上角的圖標,可以放在靜態資源路徑下或者類路徑下,靜態資源路徑優先級高。
SpringBoot啟動后默認在靜態資源目錄下尋找index.html,如果沒有找到;就會去resource/templates目錄下尋找index.html(使用Thymeleaf模板)
以上就是關于“SpringBoot靜態資源映射的方法”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注創新互聯行業資訊頻道。
當前名稱:SpringBoot靜態資源映射的方法
網站路徑:http://m.kartarina.com/article6/pihhig.html
成都網站建設公司_創新互聯,為您提供網站維護、域名注冊、全網營銷推廣、外貿建站、品牌網站設計、App開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯