springboot开发笔记-(11)-WebMVC之-01-静态资源引入

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包结构:

image.png

然后在浏览器访问: 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/, 如图:

image.png

我们浏览器访问: http://localhost:8080/list.html

image.png

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/

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 hi@niewj.com

×

喜欢就点赞,疼爱就打赏