溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot靜態資源映射的方法

發布時間:2022-03-03 15:44:59 來源:億速云 閱讀:296 作者:iii 欄目:web開發

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

靜態資源映射

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

SpringBoot靜態資源映射的方法

其中一個靜態內部類WebMvcAutoConfigurationAdapter實現了WebMvcConfigurer接口。(361)

WebMvcConfigurer接口中定義了addResourceHandlers處理靜態資源的默認映射關系.(500)

SpringBoot靜態資源映射的方法

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

返回靜態資源的默認映射關系,

SpringBoot靜態資源映射的方法

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中定義

SpringBoot靜態資源映射的方法

/

小結:

默認情況下,可以在以下五個位置放置靜態資源

classpath:/META-INF/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

/

【靜態資源一般放在classpath:/static/目錄下】

自定義favicon,自定義index.html

favicon.ico是瀏覽器左上角的圖標,可以放在靜態資源路徑下或者類路徑下,靜態資源路徑優先級高。

SpringBoot靜態資源映射的方法

SpringBoot靜態資源映射的方法

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

以上就是關于“SpringBoot靜態資源映射的方法”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女