SpringBoot如何通過webjars管理靜態(tài)資源文件夾
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包的方式引入靜態(tài)資源
什么是webjar?
搜索webjar, 可以將jquery用pom引入:
引入, 正好對應(yīng)這個映射:
結(jié)果是的:
2) springboot對靜態(tài)資源的映射規(guī)則:
看代碼:
還是
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)); } } }
進(jìn)去:
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(); }
"/**"訪問當(dāng)前項目的任何資源, (靜態(tài)資源的文件夾) ,如果沒人處理,會默認(rèn)去以下幾個文件路徑下找[/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)歡迎頁面: 靜態(tài)資源文件夾下的所有index.html頁面: 被 /**映射
http://localhost:8080/ 會到以上靜態(tài)資源文件夾中找index.html頁面
源碼有變化,我沒明白回頭再看
結(jié)果:
路徑:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC 參數(shù)綁定意義及實(shí)現(xiàn)過程解析
這篇文章主要介紹了SpringMVC 參數(shù)綁定意義及實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11JavaWeb搭建網(wǎng)上圖書商城畢業(yè)設(shè)計
這篇文章主要介紹了JavaWeb搭建網(wǎng)上圖書商城框架,特別適合正在為網(wǎng)上商城畢業(yè)設(shè)計煩惱的同學(xué),需要的朋友可以參考下2015-11-11使用Java實(shí)現(xiàn)將ppt轉(zhuǎn)換為文本
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)將ppt轉(zhuǎn)換為文本,文中的示例代碼簡潔易懂,具有一定的借鑒價值,有需要的小伙伴可以參考下2024-01-01java實(shí)現(xiàn)切割wav音頻文件的方法詳解【附外部jar包下載】
這篇文章主要介紹了java實(shí)現(xiàn)切割wav音頻文件的方法,結(jié)合實(shí)例形式詳細(xì)分析了java切割wav音頻文件的相關(guān)原理、操作技巧與注意事項,并附帶外部jar包供讀者下載,需要的朋友可以參考下2019-05-05java實(shí)現(xiàn)簡單的圖書管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單的圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07