色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

SpringBoot如何通過webjars管理靜態資源文件夾

瀏覽:31日期:2023-04-12 18:44:13

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引入:

SpringBoot如何通過webjars管理靜態資源文件夾

引入, 正好對應這個映射:

SpringBoot如何通過webjars管理靜態資源文件夾

結果是的:

SpringBoot如何通過webjars管理靜態資源文件夾

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 找

SpringBoot如何通過webjars管理靜態資源文件夾

SpringBoot如何通過webjars管理靜態資源文件夾

或者:

/resources/a/b.js找:

SpringBoot如何通過webjars管理靜態資源文件夾

SpringBoot如何通過webjars管理靜態資源文件夾

或者類路徑下/static/a/b.js找:

SpringBoot如何通過webjars管理靜態資源文件夾

SpringBoot如何通過webjars管理靜態資源文件夾

或者/public/a/b.js下找

SpringBoot如何通過webjars管理靜態資源文件夾

SpringBoot如何通過webjars管理靜態資源文件夾

3)歡迎頁面: 靜態資源文件夾下的所有index.html頁面: 被 /**映射

http://localhost:8080/ 會到以上靜態資源文件夾中找index.html頁面

源碼有變化,我沒明白回頭再看

結果:

SpringBoot如何通過webjars管理靜態資源文件夾

路徑:

SpringBoot如何通過webjars管理靜態資源文件夾

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美日韩黄色 | 亚洲国产成人精品激情 | 日本aaa成人毛片 | 欧美首页 | 97人摸人人澡人人人超一碰 | 欧美激情成人网 | 免费人成在线 | 亚洲天堂中文网 | 欧美成人欧美激情欧美风情 | 欧美午夜精品久久久久免费视 | 成人欧美在线观看 | 美女被免费视频网站a国产 美女被免费网站视频软件 美女被免费网站在线软件 美女被免费网站在线视频软件 | 在线看片 在线播放 | 在线一区二区观看 | 在线免费视频国产 | 男人天堂新地址 | 日韩一级a毛片欧美区 | se94se最新网站| 完整日本特级毛片 | 欧美一区二区不卡视频 | 99re久久精品国产首页2020 | 三级网站免费观看 | 日本在线观看一级高清片 | 成人www视频网站免费观看 | 欧美成人午夜 | 中文字幕一区视频一线 | 亚洲男人天堂手机版 | 91tv成人影院免费 | 国产精品私人玩物在线观看 | 亲子乱子伦xxxx | 美日韩一级 | 久久99一区 | 亚洲va中文字幕欧美不卡 | 91国内精品久久久久免费影院 | 国产国语高清在线视频二区 | 国产成人亚洲精品无广告 | 久久久久视频精品网 | 久草精品视频 | 免费播放毛片 | 日本亚州在线播放精品 | 国产高清精品一区 |