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

springboot静态资源的配置

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

springboot静态资源的配置

1. springboot默认的静态资源存放路径

静态资源的存放路径为classpath,也就是resources目录下的:

  • /META-INF/resources
  • /resources
  • /static
  • /public

如下所示的CLASSPATH_RESOURCE_LOCATIONS数组存放的是静态资源的访问路径。

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;
    ......
2.静态资源的访问顺序

默认情况下是按照存放静态资源路径的数组顺序访问的。也即按照下面的访问顺序:

  • /META-INF/resources
  • /resources
  • /static
  • /public


如上图所示,在这种情况下,访问index.html。那么访问的是- /META-INF/resources里面的index.html。

结论:springboot会查找优先级高的文件,从高到低,一直找到所需要的静态资源为止。

3.配置springboot项目首页

静态资源文件夹下的所有 index.html 被称为静态首页或者欢迎页,它们会被 /** 映射,换句话说就是,当我们访问"localhost:8080"时,都会跳转到静态首页(欢迎页)。

静态首页映射的原理是Spring Boot去扫描静态资源目录下的index.html页面,同时遵循静态资源优先级原则。

4.springboot 配置
# 默认值为    /**
spring.mvc.static-path-pattern=
# 默认值为   classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ 
spring.resources.static-locations=这里设置要指向的路径,多个使用英文逗号隔开

spring.mvc.static-path-pattern指定了访问项目静态资源的url地址,默认是/**。
spring.resources.static-locations指定了静态资源的存放位置。

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

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

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