【Spring Boot】静态资源导入和首页设置


一、静态资源导入

1.源码分析

查看源码:WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter中有关资源处理的方法addResourceHandlers如下:

        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();
                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));
                }

                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));
                }

            }
        }

解析:整个方法就是一个大的if-else的结构

if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } 

这里if表示我们的配置文件中有添加设置,打印日志:禁用默认资源处理:
yaml中添加静态资源设置:

spring:
  mvc:
    static-path-pattern: XXX

else里面:

  • 缓存控制
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
  • 可以访问/webjars/**下的静态文件,
  • Webjars本质就是以jar包的方式引入我们的静态资源 , 我们以前要导入一个静态资源文件,直接导入即可。可以访问网站:https://www.webjars.com/找的需要的jar包。
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));
                }

这里导入jQuery的jar包为例:
导入webjars下的jQuery

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

找到源代码中classpath:/META-INF/resources/webjars/路径
在这里插入图片描述
访问jquery.js:
在这里插入图片描述

  • else里面的另一个if语句:
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));
                }

再查看getResourceLocations的源码,就可以看到可以被加载静态资源的路径:
在这里插入图片描述

{"classpath:/META-INF/resources/",
 "classpath:/resources/", 
 "classpath:/static/",
  "classpath:/public/" }
  • web项目中resource目录下
  • 项目中的resource/resource目录下
  • 项目中的resource/static目录下
  • 项目中的resource/public目录下

2.小结

静态资源加载:

  1. 自定义目录,其他配置目录将全部失效(resource/static,resource/public…)
spring:
  mvc:
    static-path-pattern: classpath:/code1/,classpath:/code2/

配置后有效的只有:resource/code1和resource/code2

  1. webjars静态加载(一般不用)
  2. 不自定义目录,可以放静态资源的目录,可以直接访问http://localhost:8080/XXX.js
  • web项目中resource目录下
  • 项目中的resource/resource目录下
  • 项目中的resource/static目录下
  • 项目中的resource/public目录下
  • 优先级resource>static>public

二、设置首页

springboot中我们直接访问http://localhost:8080/的页面为:
在这里插入图片描述
我们如何来设置:

源码分析

WebMvcAutoConfiguration下:

private Optional<Resource> getWelcomePage() {
            String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
            return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
        }

        private Resource getIndexHtml(String location) {
            return this.resourceLoader.getResource(location + "index.html");
        }

可以看出会加载静态资源中的index.html,之前是因为没有index.html,所有页面才会显示Error Page

我们再resource/public目录下新建一个index.html(resource/static等等)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>

再次访问http://localhost:8080/:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值