SpringBoot靜態(tài)資源映射的方法

這篇“SpringBoot靜態(tài)資源映射的方法”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“SpringBoot靜態(tài)資源映射的方法”文章吧。

成都創(chuàng)新互聯(lián)專注于建始企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),商城建設(shè)。建始網(wǎng)站建設(shè)公司,為建始等地區(qū)提供建站服務(wù)。全流程按需搭建網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

靜態(tài)資源映射

SpringBoot對于SpringMVC的自動化配置都在WebMVCAutoConfiguration類中。

SpringBoot靜態(tài)資源映射的方法

其中一個靜態(tài)內(nèi)部類WebMvcAutoConfigurationAdapter實(shí)現(xiàn)了WebMvcConfigurer接口。(361)

WebMvcConfigurer接口中定義了addResourceHandlers處理靜態(tài)資源的默認(rèn)映射關(guān)系.(500)

SpringBoot靜態(tài)資源映射的方法

addResourceHandlers在WebMvcAutoConfigurationAdapter類中實(shí)現(xiàn)

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

返回靜態(tài)資源的默認(rèn)映射關(guān)系,

SpringBoot靜態(tài)資源映射的方法

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/

第五個默認(rèn)的資源映射:在靜態(tài)方法getResourceLocations中定義

SpringBoot靜態(tài)資源映射的方法

/

小結(jié):

默認(rèn)情況下,可以在以下五個位置放置靜態(tài)資源

classpath:/META-INF/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

/

【靜態(tài)資源一般放在classpath:/static/目錄下】

自定義favicon,自定義index.html

favicon.ico是瀏覽器左上角的圖標(biāo),可以放在靜態(tài)資源路徑下或者類路徑下,靜態(tài)資源路徑優(yōu)先級高。

SpringBoot靜態(tài)資源映射的方法

SpringBoot靜態(tài)資源映射的方法

SpringBoot啟動后默認(rèn)在靜態(tài)資源目錄下尋找index.html,如果沒有找到;就會去resource/templates目錄下尋找index.html(使用Thymeleaf模板)

以上就是關(guān)于“SpringBoot靜態(tài)資源映射的方法”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前名稱:SpringBoot靜態(tài)資源映射的方法
網(wǎng)站路徑:http://muchs.cn/article6/pihhig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、域名注冊、全網(wǎng)營銷推廣外貿(mào)建站、品牌網(wǎng)站設(shè)計(jì)、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司