SpringBoot:静态资源映射规则解析

WebMvcAutoConfiguration

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
        this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
        this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
            registration.addResourceLocations(this.resourceProperties.getStaticLocations());
            if (this.servletContext != null) {
                ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
                registration.addResourceLocations(new Resource[]{resource});
            }
        });
    }
}

1)、所有/webjars/**,都去classpath:/META-INF/resources/webjars/找资源;

webjars:以jar包的方式引入静态资源

https://www.webjars.org/
SpringBoot:静态资源映射规则解析
localhost:8080/webjars/jquery/3.6.0/jquery.js

在访问的时候只需要写webjars下面资源的名称即可

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.6.0</version>
</dependency>

2)"/**"访问当前项目的任何资源(静态资源的文件夹)

"classpath:/META-INF/resources/", 
"classpath:/resources/", 
"classpath:/static/", 
"classpath:/public/"
"/":当前项目的根路径

localhost:8080/abc === 去静态资源文件夹里找abc

3)欢迎页:静态资源文件夹下的所有index.html页面,被"/**"映射

localhost:8080/ 找index页面

4)所有的../favicon.ico 都是在静态资源文件夹下找

5)修改静态资源默认加载位置

spring.web.resources.static-locations=classpath:/hello/,classpath:/test/

上一篇:虚拟机安装失败,出现 “ setup failed to generate the ssl keys necessary to run vmware ” 错误


下一篇:数据结构学习,哈希表(开放定址法)