SpringBoot 静态资源目录

一、静认态资源目录

WebMvcAutoConfiguration 类自动为我们注册了如下目录为静态资源目录,也就是说直接可访问到资源的目录。

classpath:/META-INF/resources/        //  /src/java/resources/META-INF/resources/ 
classpath:/resources/    //  /src/java/resources/resources/
classpath:/static/     //  /src/java/resources/static
classpath:/public/    //  /src/java/resources/public/
/:项目根路径    //不常用

我们在以上五个目录下放静态资源(比如:a.js等),可以直接访问(http://localhost:8080/a.js),类似于以前 web 项目的 webapp 下;放到其他目录下则无法直接被访问。

优先级:

classpath:/META-INF/resources/ > classpath:/resources/ > classpath:/static/ > classpath:/public/ > /:项目根路径

分析源码:

// staticPathPattern是/**
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
    customizeResourceHandlerRegistration(
            registry.addResourceHandler(staticPathPattern)
                    .addResourceLocations(
                            this.resourceProperties.getStaticLocations())
            .setCachePeriod(cachePeriod));
}
this.resourceProperties.getStaticLocations()
========>
ResourceProperties
public String[] getStaticLocations() {
    return this.staticLocations;
}
========>
private String[] staticLocations = RESOURCE_LOCATIONS;
========>
private static final String[] RESOURCE_LOCATIONS;
private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };
========>
static {
    // 可以看到如下是对上面两个数组进行复制操作到一个新数组上,也就是合并。
    RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
            + SERVLET_RESOURCE_LOCATIONS.length];
    System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
            SERVLET_RESOURCE_LOCATIONS.length);
    System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
            SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
}


// 上述代码可以翻译为如下:
registry.addResourceHandler("/**").addResourceLocations(
    "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/", "/")
    // 设置缓存时间
    .setCachePeriod(cachePeriod));

二、默认首页

默认首页就是直接输入 “ip:port/应用上下文路径” 默认进入的页面。
WebMvcAutoConfiguration 类自动为我们注册了如下文件为默认首页。

classpath:/META-INF/resources/index.html
classpath:/resources/index.html
classpath:/static/index.html 
classpath:/public/index.html
/index.html

优先级从上到下。
输入ip:port/项目路径即可进入默认首页

三、访问路径

静态页面的访问路径是ip:port/项目路径/xxx.html
在这里插入图片描述
如上图所示,三个静态文件的访问路径分别是(注:本项目的contextpath是/01):

localhost:8080/01/kb.jpg
localhost:8080/01/index.html
localhost:8080/01/lbj.png

注:如果写成localhost:8080/01/static/index.html会报错

六、自定义过滤规则和静态资源位置

SpringBoot 默认会指定静态资源的位置(5个位置:classpath:/META-INF/resources/ classpath:/resources/ 、classpath:/static/、classpath:/public/ 、/)。

我们可以根据需求进行自定义静态资源位置和访问路径规则。

(1)代码方式

@Configuration 
public class ImageMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/**")
                .addResourceLocations("classpath:/images/");
    } 
  } 

WebMvcConfigurer 是 Spring 提供的一个配置 mvc 的适配器接口,里面有很多配置的方法,addResourceHandlers 就是专门处理静态资源的方法。

重启项目,输入ip:port/项目路径/image/xxx就能访问相应的静态资源。

(2)配置文件方式

配置文件 application.properties 定义格式:

spring.resources.static-locations=classpath:/static/	// 静态资源位置为 classpath:/static/ 
spring.mvc.static-path-pattern=/static/**    // 路径规则为/static/**   

重启项目,输入ip:port/项目路径/static/xxx才能访问静态资源。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值