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指定了静态资源的存放位置。

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值