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

栏目: Java · 发布时间: 7年前

内容简介:spring boot 对 web 静态资源的配置管理是通过配置类顾名思义,WebMvcAutoConfiguration 是web开发的相关配置都放在该类中的。那我们看看静态资源是如何配置的呢?从上面的代码中可以解读出两点关键信息:

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 中的项目路径如下图所示:

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

3.欢迎页面的映射

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

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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

全景探秘游戏设计艺术

全景探秘游戏设计艺术

Jesse Schell / 吕阳、蒋韬、唐文 / 电子工业出版社 / 2010-6 / 69.00元

撬开你脑子里的那些困惑,让你重新认识游戏设计的真谛,人人都可以成为成功的游戏设计者!从更多的角度去审视你的游戏,从不完美的想法中跳脱出来,从枯燥的游戏设计理论中发现理论也可以这样好玩。本书主要内容包括:游戏的体验、构成游戏的元素、元素支撑的主题、游戏的改进、游戏机制、游戏中的角色、游戏设计团队、如何开发好的游戏、如何推销游戏、设计者的责任等。 本书适合任何游戏设计平台的游戏设计从业人员或即将......一起来看看 《全景探秘游戏设计艺术》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具