【spring boot2】第4篇:spring boot对静态资源的管理

spring boot 对 web 静态资源的配置管理是通过配置类 WebMvcAutoConfiguration 来实现的。

WebMvcAutoConfiguration 的理解

顾名思义,WebMvcAutoConfiguration 是web开发的相关配置都放在该类中的。那我们看看静态资源是如何配置的呢?

addResourceHandlers 方法中对静态资源路径做了说明

public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
                return;
            }
            Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
            CacheControl cacheControl = this.resourceProperties.getCache()
                    .getCachecontrol().toHttpCacheControl();
            if (!registry.hasMappingForPattern("/webjars/**")) {
                customizeResourceHandlerRegistration(registry
                        .addResourceHandler("/webjars/**")
                        .addResourceLocations("classpath:/META-INF/resources/webjars/")
                        .setCachePeriod(getSeconds(cachePeriod))
                        .setCacheControl(cacheControl));
            }
            //staticPathPattern的值是 /**
            String staticPathPattern = this.mvcProperties.getStaticPathPattern();
            if (!registry.hasMappingForPattern(staticPathPattern)) {
                customizeResourceHandlerRegistration(
                        registry.addResourceHandler(staticPathPattern)
                                .addResourceLocations(getResourceLocations(
                                        this.resourceProperties.getStaticLocations()))
                                .setCachePeriod(getSeconds(cachePeriod))
                                .setCacheControl(cacheControl));
            }
        }

从上面的代码中可以解读出两点关键信息:

  1. 所有的"/webjars/**都去classpath:/META-INF/resources/webjars/路径下找静态资源

    • 什么是webjars :以jar包的形式引入静态资源文件
    • webjars官方网站
    • 比如我们现在要使用 jquery 框架,在webjars官网找到他的依赖放到你的项目当中即可
    <dependency>
        <groupId>org.webjars.bower</groupId>
        <artifactId>jquery</artifactId>
        <version>3.3.1</version>
    </dependency>
  2. 如果路径是/**时,就去以下

    classpath:/META-INF/resources/
    classpath:/resources/
    classpath:/static/
    classpath:/public/
    

    类路径查找资源文件,idea 中的项目路径如下图所示:
    图片描述
    那我们如何访问静态资源呢?通过下面的url可直接访问

    http://localhost:8080/asserts/css/bootstrap.min.css

    但是注意,假如你把静态资源存放在classpath:/static/,试图通过http://localhost:8080/static/asserts/css/bootstrap.min.css 来访问静态资源是访问不到的。

3.欢迎页面的映射

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

意思是只要在我们的静态资源文件夹中放有 index.html文件,就能自动访问到,比如:http://localhost:8080,静态文件目录指的是 2 中提到的目录。

如何自定义资源文件路径

我们的资源文件路径属性是有ResourceProperties中定义的。

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };

    private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
}

所以自定路径只需要覆盖 staticLocations 的默认路径即可,在 application.properties 文件中设置该路径

spring.resources.static-locations=classpath:/hello,classpath:/test #可以设置多个路径
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值