1.分析静态资源添加位置,在WebMvcAutoConfiguration类的addResourceHandlers()方法中

        @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();
            if (!registry.hasMappingForPattern("/webjars/**")) {
                customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
                        .addResourceLocations("classpath:/META-INF/resources/webjars/")
                        .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)
                        .setUseLastModified(this.resourceProperties.getCache().isUseLastModified()));
            }
            String staticPathPattern = this.mvcProperties.getStaticPathPattern();
            if (!registry.hasMappingForPattern(staticPathPattern)) {
                customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
                        .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
                        .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)
                        .setUseLastModified(this.resourceProperties.getCache().isUseLastModified()));
            }
        }

 

 

目录所在位置

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
                "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
public下的优先级最低:放公共资源
static下的优先级其次高:页面
resources下的优先级最高:上传文件


template下面的资源只能通过controller才能访问,需要thymeleaf依赖






posted on 2022-01-06 16:19  upupup-999  阅读(59)  评论(0)    收藏  举报