【串线篇】spring boot对静态资源的映射规则
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【串线篇】spring boot对静态资源的映射规则,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3351字,纯文字阅读大概需要5分钟。
内容图文
WebMvcAutoConfiguration的内部类
WebMvcAutoConfigurationAdapter
其中ResourceProperties点进去
其中addResourceHandlers方法
规定了静态资源(jQuery.js/html)的引用位置
WebMvcAuotConfiguration: @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); return; } Integer cachePeriod = this.resourceProperties.getCachePeriod(); if (!registry.hasMappingForPattern("/webjars/**")) { customizeResourceHandlerRegistration( registry.addResourceHandler("/webjars/**") .addResourceLocations( "classpath:/META‐INF/resources/webjars/") .setCachePeriod(cachePeriod)); 15 } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); //静态资源文件夹映射if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration( registry.addResourceHandler(staticPathPattern) .addResourceLocations( this.resourceProperties.getStaticLocations()) .setCachePeriod(cachePeriod)); 24 } }
// 配置欢迎页映射
@Bean
p u b l i c W e l c o m e P a g e H a n d l e r M a pp i n g welcomePageHandlerMapping(
R e s o u r c e P r o p e r t i e s resourceProperties) {
r e t u r n new W e l c o m e P a g e H a n d l e r M a pp i n g ( r e s o u r c e P r o p e r t i e s . g e t W e l c o m e P a g e (),
t h i s . m v c P r o p e r t i e s . g e t S t a t i c P a t h P a tt e r n ());
}
// 配置喜欢的图标
@ C o n f i g u r a t i o n
@ C o n d i t i o n a l O n P r o p e r t y ( v a l u e = "spring.mvc.favicon.enabled", matchIfMissing = true)
p u b l i c static class Fa v i c o n C o n f i g u r a t i o n {
p r i v a t e final R e s o u r c e P r o p e r t i e s resourceProperties;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
@Bean
p u b l i c S i m p l e U r l H a n d l e r M a pp i n g faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
// 所 有 **/favicon.ico
m a pp i n g . s e t U r l M a p ( C o ll e c t i o n s . s i n g l e t o n M a p ( " ** / f a v i c o n . i c o " , f a v i c o n R e q u e s t H a n d l e r ()));
r e t u r n m a pp i n g ;
}
@Bean
p u b l i c R e s o u r c e H tt p R e q u e s t H a n d l e r faviconRequestHandler() { ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler.setLocations(this.resourceProperties.getFaviconLocations());
return requestHandler;
}
}
1)、所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;
webjars:以jar包的方式引入静态资源(选择maven方式); http://www.webjars.org/
localhost:8080/webjars/jquery/3.3.1/jquery.js
<! ‐‐引入jquery‐webjar‐‐ > 在访问的时候只需要写webjars下面资源的名称即可 < dependency > < groupId >org.webjars</groupId><artifactId>jquery</artifactId><version>3.3.1</version></dependency>
2)、"/**" 访问当前项目的任何资源,都去(静态资源的文件夹)找映射
类路径classpath就是Java 与resources
"classpath:/META‐INF/resources/",即resources下再建/META‐INF/resources/
"classpath:/resources/",即resources下再建resources
"classpath:/static/",即resources下再建static
"classpath:/public/"即resources下再建public
"/":当前项目的根路径
localhost:8080/abc === 去静态资源文件夹里面找abc
3)、欢迎页; 静态资源文件夹下的所有index.html页面;被"/**"映射;
localhost:8080/ 找index页面
4)、所有的 **/favicon.ico 都是在静态资源文件下找;
原文:https://www.cnblogs.com/yanl55555/p/12091341.html
内容总结
以上是互联网集市为您收集整理的【串线篇】spring boot对静态资源的映射规则全部内容,希望文章能够帮你解决【串线篇】spring boot对静态资源的映射规则所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。