码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-03-01 12:47:37      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:pat   技术   映射   返回   资源   chain   rop   bool   hpa   

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 静态资源的映射规则 (2) 替他资源映射

标签:pat   技术   映射   返回   资源   chain   rop   bool   hpa   

原文地址:https://www.cnblogs.com/guangzhou11/p/12388875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!