SpringBoot如何通過webjars管理靜態資原始檔夾
WebMvcAutoConfiguration
新增資源對映:
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)); } } }
所有"/webjars/**"路徑 , 都去類路徑下 classpath: /META-INF/resources/webjars/ 找資源,
所以就是
http://localhost:8080/webjars/jquery/3.5.1/jquery.js
能訪問
/META-INF/resources/webjars/jquery/3.5.1/jquery.js 路徑的檔案
1) webjars: 以jar包的方式引入靜態資源
什麼是webjar?
搜尋webjar, 可以將jquery用pom引入:
引入, 正好對應這個對映:
結果是的:
2) springboot對靜態資源的對映規則:
看程式碼:
還是
WebMvcAutoConfiguration的這個方法
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)); } } }
進去:
WebMvcProperties
private String staticPathPattern; private final WebMvcProperties.Async async; private final WebMvcProperties.Servlet servlet; private final WebMvcProperties.View view; private final WebMvcProperties.Contentnegotiation contentnegotiation; private final WebMvcProperties.Pathmatch pathmatch; public WebMvcProperties() { this.localeResolver = WebMvcProperties.LocaleResolver.ACCEPT_HEADER; this.format = new WebMvcProperties.Format(); this.dispatchTraceRequest = false; this.dispatchOptionsRequest = true; this.ignoreDefaultModelOnRedirect = true; this.publishRequestHandledEvents = true; this.throwExceptionIfNoHandlerFound = false; this.logResolvedException = false; this.staticPathPattern = "/**"; this.async = new WebMvcProperties.Async(); this.servlet = new WebMvcProperties.Servlet(); this.view = new WebMvcProperties.View(); this.contentnegotiation = new WebMvcProperties.Contentnegotiation(); this.pathmatch = new WebMvcProperties.Pathmatch(); }
addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())) 這裡添加了資源的位置
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(); }
"/**"訪問當前專案的任何資源, (靜態資源的資料夾) ,如果沒人處理,會預設去以下幾個檔案路徑下找[/code]複製程式碼 程式碼如下:// 靜態資原始檔夾, 這幾個都可以存放靜態資源:
classpath:/META-INF/resources/classpath:/resources/"classpath:/static/"classpath:/public/
例如 localhost:8080/a/b.js , 可以到 /META-INF/resources/a/b.js 找
或者:
/resources/a/b.js找:
或者類路徑下/static/a/b.js找:
或者/public/a/b.js下找
3)歡迎頁面: 靜態資原始檔夾下的所有index.html頁面: 被 /**對映
http://localhost:8080/ 會到以上靜態資原始檔夾中找index.html頁面
原始碼有變化,我沒明白回頭再看
結果:
路徑:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。