溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2022-03-03 15:44:59 來(lái)源:億速云 閱讀:267 作者:iii 欄目:web開(kāi)發(fā)

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

靜態(tài)資源映射

SpringBoot對(duì)于SpringMVC的自動(dòng)化配置都在WebMVCAutoConfiguration類中。

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

其中一個(gè)靜態(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/

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

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

/

小結(jié):

默認(rèn)情況下,可以在以下五個(gè)位置放置靜態(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)先級(jí)高。

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

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

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

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

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI