SpringBoot靜態(tài)資源的訪問(wèn)方法詳細(xì)介紹
一. 靜態(tài)資源
在web場(chǎng)景中的靜態(tài)圖片、html網(wǎng)頁(yè)等
二. 靜態(tài)資源訪問(wèn)目標(biāo)
在SpringBoot中,靜態(tài)資源訪問(wèn)目標(biāo)有 resources文件下的 public、resources、static 以及 META-INF 文件夾下的 recources
如下圖所示:
(注意文件夾要自己創(chuàng)建,不要寫(xiě)錯(cuò)名字!??!名字是固定的,就這幾個(gè))

三. 靜態(tài)資源訪問(wèn)前綴
1. 默認(rèn)訪問(wèn)路徑為 /
放于上述文件夾下的靜態(tài)資源可以直接在根目錄下訪問(wèn)
如下:以訪問(wèn)qqVeiw.jpg為例

2. 配置訪問(wèn)前綴
為什么需要:在Request訪問(wèn)以及靜態(tài)資源訪問(wèn)同名時(shí),SpringBoot會(huì)訪問(wèn)優(yōu)先訪問(wèn)Request請(qǐng)求
因此,需要給靜態(tài)資配置訪問(wèn)前綴,配置方法非常簡(jiǎn)單,只需在yaml配置文件中加入如下:
spring:
mvc:
static-path-pattern: /res/** #靜態(tài)資源訪問(wèn)前綴為res
如下圖:

3. 配置靜態(tài)資源默認(rèn)訪問(wèn)位置
我們可以設(shè)置一個(gè)或多個(gè)自定義文件夾為靜態(tài)資源默認(rèn)訪問(wèn)位置(數(shù)組形式),只需在yaml配置文件中加入如下:
spring:
resources:
static-locations: [classpath:/haha/, classpath/static/] #在類路徑的haha文件夾下的靜態(tài)資源才能被訪問(wèn)到
四. 歡迎頁(yè)及網(wǎng)頁(yè)圖標(biāo)設(shè)置
1. 歡迎頁(yè)的設(shè)置
只需將 index.html 網(wǎng)頁(yè)加入配置的static-locations中,再去訪問(wèn)根目錄,就可以看到SpringBoot為我們配置好的歡迎頁(yè)
(1)yaml文件配置
(2)index.html加入靜態(tài)資源訪問(wèn)目標(biāo)

spring:
resources:
static-locations: [classpath:/haha/] #在類路徑的haha文件夾下的靜態(tài)資源才能被訪問(wèn)到
2. 網(wǎng)頁(yè)圖標(biāo)的設(shè)置
將命名為favicon.ico的圖標(biāo)加入靜態(tài)資源訪問(wèn)目錄


注意: 若設(shè)置了訪問(wèn)前綴,則上述兩功能不生效使用設(shè)置網(wǎng)頁(yè)圖標(biāo)功能,要開(kāi)啟禁用瀏覽器緩存功能

分析源碼
WelcomePageHandlerMapping類
1. 實(shí)現(xiàn) 歡迎頁(yè)
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.實(shí)現(xiàn)靜態(tài)資源訪問(wèn)
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類
實(shí)現(xiàn)歡迎頁(yè)
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,
ApplicationContext applicationContext, Optional<Resource> welcomePage, String staticPathPattern) {
// 從這里我們也可以看出來(lái)為什么歡迎頁(yè)不能加靜態(tài)資源訪問(wèn)前綴
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)資源的訪問(wèn)方法詳細(xì)介紹的文章就介紹到這了,更多相關(guān)SpringBoot靜態(tài)資源的訪問(wèn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文詳解Springboot中filter的原理與注冊(cè)
這篇文章主要為大家詳細(xì)介紹了Springboot中filter的原理與注冊(cè)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們掌握SpringBoot有一定的幫助,需要的可以參考一下2023-02-02
java基礎(chǔ)之接口組成更新的實(shí)現(xiàn)
本文主要介紹了java基礎(chǔ)之接口組成更新的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Spring中的DeferredImportSelector實(shí)現(xiàn)詳解
這篇文章主要介紹了Spring中的DeferredImportSelector實(shí)現(xiàn)詳解,兩個(gè)官方的實(shí)現(xiàn)類AutoConfigurationImportSelector和ImportAutoConfigurationImportSelector都是Spring Boot后新增的實(shí)現(xiàn),需要的朋友可以參考下2024-01-01
Java異常 Exception類及其子類(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇Java異常 Exception類及其子類(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Mybatis的mapper標(biāo)簽 namespace屬性用法說(shuō)明
這篇文章主要介紹了Mybatis的mapper標(biāo)簽 namespace屬性用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringMVC中的DispatcherServlet請(qǐng)求分析
這篇文章主要介紹了SpringMVC中的DispatcherServlet請(qǐng)求分析, DispatcherServlet作為一個(gè)Servlet,那么當(dāng)有請(qǐng)求到Tomcat等Servlet服務(wù)器時(shí),會(huì)調(diào)用其service方法,再調(diào)用到其父類GenericServlet的service方法,需要的朋友可以參考下2024-01-01

