SpringBoot靜態(tài)資源的訪問方法詳細(xì)介紹
一. 靜態(tài)資源
在web場景中的靜態(tài)圖片、html網(wǎng)頁等
二. 靜態(tài)資源訪問目標(biāo)
在SpringBoot中,靜態(tài)資源訪問目標(biāo)有 resources文件下的 public、resources、static 以及 META-INF 文件夾下的 recources
如下圖所示:
(注意文件夾要自己創(chuàng)建,不要寫錯名字?。?!名字是固定的,就這幾個)
三. 靜態(tài)資源訪問前綴
1. 默認(rèn)訪問路徑為 /
放于上述文件夾下的靜態(tài)資源可以直接在根目錄下訪問
如下:以訪問qqVeiw.jpg為例
2. 配置訪問前綴
為什么需要:在Request訪問以及靜態(tài)資源訪問同名時,SpringBoot會訪問優(yōu)先訪問Request請求
因此,需要給靜態(tài)資配置訪問前綴,配置方法非常簡單,只需在yaml配置文件中加入如下:
spring:
mvc:
static-path-pattern: /res/** #靜態(tài)資源訪問前綴為res
如下圖:
3. 配置靜態(tài)資源默認(rèn)訪問位置
我們可以設(shè)置一個或多個自定義文件夾為靜態(tài)資源默認(rèn)訪問位置(數(shù)組形式),只需在yaml配置文件中加入如下:
spring:
resources:
static-locations: [classpath:/haha/, classpath/static/] #在類路徑的haha文件夾下的靜態(tài)資源才能被訪問到
四. 歡迎頁及網(wǎng)頁圖標(biāo)設(shè)置
1. 歡迎頁的設(shè)置
只需將 index.html 網(wǎng)頁加入配置的static-locations中,再去訪問根目錄,就可以看到SpringBoot為我們配置好的歡迎頁
(1)yaml文件配置
(2)index.html加入靜態(tài)資源訪問目標(biāo)
spring:
resources:
static-locations: [classpath:/haha/] #在類路徑的haha文件夾下的靜態(tài)資源才能被訪問到
2. 網(wǎng)頁圖標(biāo)的設(shè)置
將命名為favicon.ico的圖標(biāo)加入靜態(tài)資源訪問目錄
注意: 若設(shè)置了訪問前綴,則上述兩功能不生效使用設(shè)置網(wǎng)頁圖標(biāo)功能,要開啟禁用瀏覽器緩存功能
分析源碼
WelcomePageHandlerMapping類
1. 實現(xiàn) 歡迎頁
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) { WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping( new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(), this.mvcProperties.getStaticPathPattern()); welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider)); welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations()); return welcomePageHandlerMapping; }
2.實現(xiàn)靜態(tài)資源訪問
public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); return; } Duration cachePeriod = this.resourceProperties.getCache().getPeriod(); CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl(); if (!registry.hasMappingForPattern("/webjars/**")) { customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern) .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())) .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } }
繼續(xù)往下走——
WelcomePageHandlerMapping類
實現(xiàn)歡迎頁
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders, ApplicationContext applicationContext, Optional<Resource> welcomePage, String staticPathPattern) { // 從這里我們也可以看出來為什么歡迎頁不能加靜態(tài)資源訪問前綴 if (welcomePage.isPresent() && "/**".equals(staticPathPattern)) { logger.info("Adding welcome page: " + welcomePage.get()); setRootViewName("forward:index.html"); } else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) { logger.info("Adding welcome page template: index"); setRootViewName("index"); } }
到此這篇關(guān)于SpringBoot靜態(tài)資源的訪問方法詳細(xì)介紹的文章就介紹到這了,更多相關(guān)SpringBoot靜態(tài)資源的訪問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解spring-boot集成elasticsearch及其簡單應(yīng)用
本篇文章主要介紹了詳解spring-boot集成elasticsearch及其簡單應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06使用Java獲取List交集數(shù)據(jù)的實現(xiàn)方案小結(jié)
今天遇到一個小需求,當(dāng)用戶上傳了一個關(guān)于用戶數(shù)據(jù)的列表,我們需要將其與數(shù)據(jù)庫中已有的用戶數(shù)據(jù)進(jìn)行比較,所以本文給大家介紹了使用Java獲取List交集數(shù)據(jù)的實現(xiàn)方案小結(jié),文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-03-03mybatis?plus?MetaObjectHandler?不生效的解決
今天使用mybatis-plus自動為更新和插入操作插入更新時間和插入時間,配置了MetaObjectHandler不生效,本文就來解決一下,具有一定的 參考價值,感興趣的可以了解一下2023-10-10java socket實現(xiàn)聊天室 java實現(xiàn)多人聊天功能
這篇文章主要為大家詳細(xì)介紹了java socket實現(xiàn)聊天室,java實現(xiàn)多人聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07Java實現(xiàn)字符串轉(zhuǎn)換成可執(zhí)行代碼的方法
今天小編就為大家分享一篇Java實現(xiàn)字符串轉(zhuǎn)換成可執(zhí)行代碼的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07