栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot静态资源访问实现代码解析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot静态资源访问实现代码解析

springboot静态资源加载默认是从/static(或/public或/resources或/meta-INF/resources) 目录下加载静态资源。

加载的优选级别:/meta-INF/resources》/resources》/public》/static

静态资源的加载源码分析(WebMvcAutoConfiguration类)

首先从WebMvcAutoConfiguration.class自动配置类部分代码来看:

//添加静态资源规则
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();
 //webjars依赖映射规则
 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));
 }
				//本地配置的映射规则
 //this.resourceProperties.getStaticLocations() 从ResourceProperties中加载静态路径
 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));
 }

      }
    }

ResourceProperties类部分源码

@ConfigurationProperties(
  prefix = "spring.resources",
  ignoreUnknownFields = false
)
public class ResourceProperties {
  //springboot默认的加载路径
  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;

映射规则总结

在springboot静态资源加载规则中,除了”标准“静态资源位置之外,还有一个较为特殊的WebJars

“标准”静态资源映射规则

​所有的“/**”的请求,如果没有对应的处理,那么就去默认映射的静态资源目录下去找,如下所示:

  1. "classpath:/meta-INF/resources/"
  2. "classpath:/resources/"
  3. "classpath:/static/",
  4. "classpath:/public/"
  5. “/**”

所有的webjars的请求都会去 ”classpath:/meta-INF/resources/webjars/**“去资源

(如果 以jar包的方式来引入jquery包)

在pom.xml中引入依赖

 
      org.webjars
      jquery
      3.3.1-2
    

从引入的包目录来看

springboot默认欢迎页面

自动去加载默认目录下的index.html;如static/index.html

自定义配置静态资源目录

在application.properties文件中去配置

//配置test为静态资源目录
spring.resources.static-locations=classpath:/test/

遇到的坑

在配置了静态资源目录的时候,跳转到的页面路径不能写绝对路径,

比如:spring.resources.static-locations=classpath:/test/ 我配置test为静态资源的加载位置,在访问的时候不需要写test

请求:http://127.0.0.1:8085/test/1.png

请求:http://127.0.0.1:8085/1.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/133852.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号