关于springboot:springboot开发笔记11WebMVC之01静态资源引入

37次阅读

共计 2847 个字符,预计需要花费 8 分钟才能阅读完成。

上一节:pringboot 开发笔记 -(10)- 日志框架 SLF4J 和 logback 等


11.1 动态资源引入源码解析

动态资源引入, springboot webmvc 在 WebMvcAutoConfiguration 源码中有两种形式:

  1. webjars 引入动态资源:

    映射形式: /webjars/** ======> classpath:/META-INF/resources/webjars

  2. /** 指定的几个目录

    映射形式: "/**" ======> classpath: [/META-INF/resources/, /resources/, /static/, /public/]

    1. META-INF/resources/
    2. /resources/
    3. /static/
    4. /public/

详见: WebMvcAutoConfiguration 源码的解析:

@Override
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();
    // #1. "webjars" 目录映射 => classpath:/META-INF/resources/webjars/
    if (!registry.hasMappingForPattern("/webjars/**")) {customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
                // 对应下面的 jquery.js 目录:
                .addResourceLocations("classpath:/META-INF/resources/webjars/")
                .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }
    // #2."/**" 目录映射 => classpath:[/META-INF/resources/, /resources/, /static/, /public/]
    String staticPathPattern = this.mvcProperties.getStaticPathPattern();
    if (!registry.hasMappingForPattern(staticPathPattern)) {customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)                            .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
            .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }
}

// 下面 23 行的: this.resourceProperties.getStaticLocations():

@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/" };

    /**
     * Locations of static resources. Defaults to classpath:[/META-INF/resources/,
     * /resources/, /static/, /public/].
     */
    private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
    .....
}    

11.2 用 webjars 形式引入动态资源

webjars 形式引入 jquery: pom:

<!-- webjars 形式引入 jquery -->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.5.1</version>
</dependency>

引入后 jquery.js 所在 jar 包构造:

而后在浏览器拜访: http://localhost:8080/webjars/jquery/3.5.1/jquery.js 即可拜访 jquery.js 的源码;

11.3 用 static 目录等形式引入动态资源

间接将动态文件 copy 到四个目录之一都能够

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

11.3.1 classpath:/META-INF/resources

11.3.2 classpath:/resources/

11.3.3 classpath:/static/

11.3.4 classpath:/public/

咱们应用 classpath: /static/, 如图:

咱们浏览器拜访: http://localhost:8080/list.html

OK!!

11.4 小结

springboot webmvc 拜访动态资源有 2 种形式: 引入 maven 依赖的 webjars 形式和动态资源默认四个目录形式.

  1. webjars 形式: www.webjars.com 找到 webjars 依赖的 pom, 导入, 而后依据目录拜访即可!
  2. /** 的四种默认地址: classpath:[/META-INF/resources, /resources/, /static/, /public/]

    当然下面的 /** 也能够本人指定一个默认动态资源的地址, 指定形式:

    spring.resources.static-locations=classpath:/hello1/, classpath:/hello2/

正文完
 0