SpringBoot如何通過(guò)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/**'路徑 , 都去類(lèi)路徑下 classpath: /META-INF/resources/webjars/ 找資源, 所以就是
http://localhost:8080/webjars/jquery/3.5.1/jquery.js
能訪問(wèn)
/META-INF/resources/webjars/jquery/3.5.1/jquery.js 路徑的文件
1) webjars: 以jar包的方式引入靜態(tài)資源
什么是webjar?
搜索webjar, 可以將jquery用pom引入:
引入, 正好對(duì)應(yīng)這個(gè)映射:
結(jié)果是的:
2) springboot對(duì)靜態(tài)資源的映射規(guī)則:
看代碼:
還是
WebMvcAutoConfiguration的這個(gè)方法
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(); }
'/**'訪問(wèn)當(dāng)前項(xiàng)目的任何資源, (靜態(tài)資源的文件夾) ,如果沒(méi)人處理,會(huì)默認(rèn)去以下幾個(gè)文件路徑下找[/code]復(fù)制代碼 代碼如下:// 靜態(tài)資源文件夾, 這幾個(gè)都可以存放靜態(tài)資源:
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找:
或者類(lèi)路徑下/static/a/b.js找:
或者/public/a/b.js下找
3)歡迎頁(yè)面: 靜態(tài)資源文件夾下的所有index.html頁(yè)面: 被 /**映射
http://localhost:8080/ 會(huì)到以上靜態(tài)資源文件夾中找index.html頁(yè)面
源碼有變化,我沒(méi)明白回頭再看
結(jié)果:
路徑:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 解決Android Studio 格式化 Format代碼快捷鍵問(wèn)題2. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問(wèn)題3. 在Chrome DevTools中調(diào)試JavaScript的實(shí)現(xiàn)4. Springboot 全局日期格式化處理的實(shí)現(xiàn)5. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)6. Java使用Tesseract-Ocr識(shí)別數(shù)字7. vue實(shí)現(xiàn)web在線聊天功能8. JAMon(Java Application Monitor)備忘記9. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法10. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼
