SpringBoot的静态资源

静态资源映射规则

介绍

可以直接访问(http://localhost:8080/a.js),类似于以前web项目的webapp下;放到其他目录下无法被访问。

对哪些目录映射

优先级从上到下。

  1. classpath:/META-INF/resources/
  2. classpath:/resources/
  3. classpath:/static/
  4. classpath:/public/
  5. /:当前项目的根路径

为什么是这些目录

读下源码,这个是SpringBoot自动配置的WebMvcAutoConfiguration.java类来帮我们干的。WebMvcAutoConfiguration类自动为我们注册了如下目录为静态资源目录,也就是说直接可访问到资源的目录。

// 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/项目名称默认进入的页面。

分析源码

resourceProperties.getWelcomePage()
========>
public Resource getWelcomePage() {
    // 遍历默认静态资源目录后面拼接个index.html的数组
    // 比如:[/static/index.html, /public/index.html等等]
    for (String location : getStaticWelcomePageLocations()) {
        Resource resource = this.resourceLoader.getResource(location);
        try {
            if (resource.exists()) {
                resource.getURL();
                return resource;
            }
        }
        catch (Exception ex) {
            // Ignore
        }
    }
    return null;
}
========>
// 下面这段代码通俗易懂,就是给默认静态资源目录后面拼接个index.html并返回,比如:/static/index.html
private String[] getStaticWelcomePageLocations() {
    String[] result = new String[this.staticLocations.length];
    for (int i = 0; i < result.length; i++) {
        String location = this.staticLocations[i];
        if (!location.endsWith("/")) {
            location = location + "/";
        }
        result[i] = location + "index.html";
    }
    return result;
}

总结

WebMvcAutoConfiguration类自动为我们注册了如下文件为默认首页。

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

优先级从上到下。

favicon.ico

介绍

就是如图所示:
在这里插入图片描述

源码分析

// 首先可以看到的是可以设置是否生效,通过参数spring.mvc.favicon.enabled来配置,若无此参数,则默认是生效的。
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
========// 可以看到所有的**/favicon.ico都是在faviconRequestHandler()这个方法里找。
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
========faviconRequestHandler().this.resourceProperties.getFaviconLocations()
// 就是之前的五个静态资源文件夹。    
List<Resource> getFaviconLocations() {
    List<Resource> locations = new ArrayList<Resource>(
            this.staticLocations.length + 1);
    if (this.resourceLoader != null) {
        for (String location : this.staticLocations) {
            locations.add(this.resourceLoader.getResource(location));
        }
    }
    locations.add(new ClassPathResource("/"));
    return Collections.unmodifiableList(locations);
}

总结

只要把favicon.ico放到以上目录下,就会自动生效。也区分优先级。

webjars

介绍

就是以jar包的方式引入静态资源。官网地址:http://www.webjars.org/。类似于maven仓库。

分析源码

Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
    customizeResourceHandlerRegistration(
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations(
                            "classpath:/META-INF/resources/webjars/")
            .setCachePeriod(cachePeriod));
}

总结

所有/webjars/**都从classpath:/META-INF/resources/webjars/路径下去找对应的静态资源。

案例

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

看项目依赖
在这里插入图片描述
会自动为我们引入jquery,要怎么使用呢?我们上面说过:
所有/webjars/*都从classpath:/META-INF/resources/webjars/路径下去找对应的静态资源。
所以我们启动项目,访问:http://localhost:8080/webjars/jquery/3.3.1/jquery.js即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值