spring boot 静态资源的映射规则 替他资源映射

Posted 1点

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 静态资源的映射规则 替他资源映射相关的知识,希望对你有一定的参考价值。

1.如果不是    /webjars/** 资源

if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

spring boot 会通过这个 方法   getStaticLocations 查找

   public String[] getStaticLocations() {
        return this.staticLocations;
    }

通过这个 方法返回    staticLocations 资源文件

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
    private String[] staticLocations;
    private boolean addMappings;
    private final ResourceProperties.Chain chain;
    private final ResourceProperties.Cache cache;

    public ResourceProperties() {
        this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
        this.addMappings = true;
        this.chain = new ResourceProperties.Chain();
        this.cache = new ResourceProperties.Cache();
    }

从这里可以看出是一个数组

"classpath:/META-INF/resources/", 

"classpath:/resources/",

"classpath:/static/",

"classpath:/public/"}

所以在项目中 我们可以将静态文件放入 这些文件夹中

 

 

实例:

 

 

 

 

 

 

 

 

以上是关于spring boot 静态资源的映射规则 替他资源映射的主要内容,如果未能解决你的问题,请参考以下文章

spring boot 静态资源的映射规则 欢迎页面映射

Spring Boot对静态资源的映射规则

Spring Boot自定义静态资源映射

spring boot 静态资源映射

使用 Spring Boot 映射 html 资源

Spring Boot 2从入门到入坟 | Web场景开发篇:静态资源规则与定制化