用代码解析Springboot静态资源访问

发布时间:2020-07-18 11:30:21 作者:小猪
来源:亿速云 阅读:236

这篇文章主要用代码解析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中引入依赖

 <dependency>
      <groupId>org.webjars</groupId>
      <artifactId>jquery</artifactId>
      <version>3.3.1-2</version>
    </dependency>

从引入的包目录来看

用代码解析Springboot静态资源访问

springboot默认欢迎页面

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

自定义配置静态资源目录

在application.properties文件中去配置

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

遇到的坑

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

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

用代码解析Springboot静态资源访问

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

用代码解析Springboot静态资源访问

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

用代码解析Springboot静态资源访问

以上就是关于用代码解析Springboot静态资源访问的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。

推荐阅读:
  1. SpringBoot配置Jsp和静态资源访问
  2. 用代码解析SpringBoot基于Shiro处理ajax请求

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot 静态资源访问

上一篇: 二叉树的中序、先序、后序遍历非递归遍历算法(使用堆栈,用循环实现)

下一篇:查看mysql服务是否启动的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》