SpringBoot2.x入门到项目实战课程系列(第五章)( 二 )
1、所有 /webjars/** 请求 , 都去 classpath:/META-INF/resources/webjars/ 目录找对应资源文件
2、webjars:以jar包的方式引入静态资源;webjars官网:
3、在官网打开资源文件的依赖配置信息 , 然后粘贴到 pom.xml 中
4、访问 localhost:8080/webjars/jquery/3.3.1/jquery.js 会在下面路径 中查找
文章插图
5.2.2 其他静态资源映射
- 在 WebMvcAuotConfiguration.addResourceHandlers() 分析 访问其他资源映射
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:/METAINF/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(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl)); }}- staticPathPattern 处理其他访问的静态路径 ,从 WebMVCProperties 构造器中获取到 /**
public WebMvcProperties() { this.localeResolver = WebMvcProperties.LocaleResolver.ACCEPT_HEADER; this.dispatchTraceRequest = false; this.dispatchOptionsRequest = true; this.ignoreDefaultModelOnRedirect = 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();}- ResourceProperties 根据请求查找资源文件, 从以下 四个路径 中 查找( 静态资源目录 )
@ConfigurationProperties(prefix = "spring.resources",ignoreUnknownFields = false)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;"classpath:/META-INF/resources/","classpath:/resources/","classpath:/static/","classpath:/public/"
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 徐福记联手JDL京东物流向数智化转型,首次落地智慧园区项目
- 苹果两款新iPad齐曝光:性能提高、入门款更轻薄、售价便宜
- 运动计数开发项目的对抗赛:飞算全自动软件工程平台碾压传统模式
- RHEL 9提升了x86_64处理器的入门要求
- 江北新区企业院士工作站技术攻关项目立项数位居南京第一
- 入门HiFi享好声,这几款耳机绝对值得入手
- 赞!盐城高新区4个项目入选省级项目立项!
- 威海高新区2项目获2020年度山东省重点研发计划(重大科技创新工程)立项支持
- 浙江公布首批“互联网+”医疗服务价格项目!适用各级公立医疗机构
- 168天封顶!投资56亿重点AI项目将在上海竣工,提供世界领先算力支撑
