SpringBoot訪問靜態(tài)資源的配置及順序說明
訪問靜態(tài)資源的配置及順序
今天在玩SpringBoot的demo的時候,放了張圖片在resources目錄下,啟動區(qū)訪問的時候,突然好奇是識別哪些文件夾來展示靜態(tài)資源的, 為什么有時候放的文件夾不能顯示,有的卻可以.
1. SpringBoot的默認配置
首先我們打開WebMvcAutoConfiguration類, 因為是靜態(tài)資源的位置, 所以搜索location,找到這一行代碼:
?String staticPathPattern = this.mvcProperties.getStaticPathPattern();
? ? if (!registry.hasMappingForPattern(staticPathPattern)) {
? ? ? this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
? ? }然后進入getStaticLocations這個方法,到了ResourceProperties類中的
? public String[] getStaticLocations() {
? ? return this.staticLocations;
? }這個方法,那接著看staticLocations這個屬性,其實就到了這個類的頂部
public class ResourceProperties {
? private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
? private String[] staticLocations;
? private boolean addMappings;
? private final ResourceProperties.Chain chain;
? private final ResourceProperties.Cache cache;
? public ResourceProperties() {
? ? this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
? ? this.addMappings = true;
? ? this.chain = new ResourceProperties.Chain();
? ? this.cache = new ResourceProperties.Cache();
? }
...
}可以看出,靜態(tài)資源默認的位置是classpath,也就是resource目錄下的:
- /META-INF/resources
- /resources
- /static
- /public
而且順序就是數組的順序.
2. 測試
我們創(chuàng)建一個index.html頁面,然后<h1>標簽分別是各自的路徑,比如在/META-INF/resources下的index.html:
<!DOCTYPE html > <html> <head lang="en"> ? ? <meta charset="UTF-8" /> ? ? <title>http-template</title> </head> <body> <h1 >META-INF.resources</h1> </body> </html>
在上面四個文件夾中各自放一個不同標題的頁面,啟動springboot,訪問localhost:8080
可以看到頁面的標題是/META-INF/resources, 說明是按照上面的默認配置讀取順序讀取的

注意,這里不需要加這個靜態(tài)資源文件夾的名字!!,比如localhost:8080能看到頁面,但是localhost:8080/static或localhost:8080/META-INF/resources是訪問不了的
為了繼續(xù)證實四個文件夾都可以,我放了同一個圖片在各自文件夾,只是名字不同,結構如下:

訪問
- localhost:8080/2b.jpg
- localhost:8080/3b.jpg
- localhost:8080/4b.jpg
- localhost:8080/5b.jpg
都能訪問(不需要加靜態(tài)資源文件夾的名字!!!), 反而是resource根目錄,也就是classpath下的1b.jpg不能訪問
3.配置
配置一: 是否可以訪問靜態(tài)資源
spring:
mvc:
static-path-pattern: /static/**
這個配置默認是/**, 表示的是正則匹配到這種路徑才去訪問靜態(tài)資源,所以默認情況下,上面四個能夠訪問的路徑也必須加上/zgd才可以訪問
另一個需要注意的事,默認情況下訪問index.html頁面,不需要加這個文件名,比如localhost:8080,但是配置了該項后,需要文件名.localhost:8080/zgd/index.html

加上index.html資源全稱后可以訪問

配置二: 去哪找靜態(tài)資源
這個配置就是我們上面說的那四個文件夾的配置了,注釋掉上面的配置,啟動看看
spring:
resources:
static-locations: classpath:/static/
啟動訪問localhost:8080
不出意料的顯示的是static的標題,也就是static文件夾下的html文件.

此時嘗試訪問3b.jpg,4b.jpb都是無法訪問的,只能訪問2b.jpg
靜態(tài)資源的配置心得
在springboot的application.properties中,下面2個配置是配置靜態(tài)資源的。
# 默認值為 /** spring.mvc.static-path-pattern= # 默認值為 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ spring.resources.static-locations=這里設置要指向的路徑,多個使用英文逗號隔開
上面的那個是匹配規(guī)則,換句話說,只有靜態(tài)資源滿足什么樣的匹配條件,Spring Boot才會處理靜態(tài)資源請求。
下面的是匹配后指向的路徑。也就是說 ,這個配置項是告訴springboot去哪找資源。
舉例
spring.mvc.static-path-pattern=/gw/**
如果原先訪問首頁的地址是:http://localhost:8080/index.html,那么在你配置這個配置后,上面的訪問就失效了,現在訪問同樣的頁面需要這樣訪問:http://localhost:8888/gw/index.html
配置訪問路徑為/gw/后原本可以訪問resources下其他文件夾的目錄下的靜態(tài)資源,現在不可以了。
比如下面截圖中pay.html不能訪問了,在沒有配置為/static/可以通過http://localhost:8080/pay.html訪問,注意地址欄。(測試時,spring.mvc.static-path-pattern=/static/**)


實際springboot項目,一般會將js、css等靜態(tài)文件放在static下,而將頁面放在templates下。

使用:

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Cloud Gateway 記錄請求應答數據日志操作
這篇文章主要介紹了Spring Cloud Gateway 記錄請求應答數據日志操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實現數據庫訪問功能
這篇文章主要介紹了SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實現數據庫訪問功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
詳談jvm--Java中init和clinit的區(qū)別
下面小編就為大家?guī)硪黄斦刯vm--Java中init和clinit的區(qū)別。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
java 中序列化與readResolve()方法的實例詳解
這篇文章主要介紹了java 中序列化與readResolve()方法的實例詳解的相關資料,這里提供實例幫助大家理解這部分知識,需要的朋友可以參考下2017-08-08

