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

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

WebMvcAutoConfiguration 的理解

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

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

从上面的代码中能够解读出两点关键信息:jquery

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

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

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

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

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

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

3.欢迎页面的映射app

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

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

如何自定义资源文件路径

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

@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 #能够设置多个路径
相关文章
相关标签/搜索