SpringBoot静态资源访问

SpringBoot静态资源访问

常见问题:
一、idea快捷方式创建的项目,templates文件夹底下存放的静态文件,无法通过localhost:8080直接访问。
原因:
WebMvcAutoConfigurationAdapter implements WebMvcConfigurer中:

public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                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));
                }

            }
        }

其中

this.resourceProperties.getStaticLocations();

将路径获取指向了ResourceProperties中的变量

这里初始化静态文件扫描路径的时候,默认被扫描的路径如下

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};


WebMvcAutoConfiguration.getResourceLocations();
对4个静态位置进行扩充

static String[] getResourceLocations(String[] staticLocations) {
        String[] locations = new String[staticLocations.length + SERVLET_LOCATIONS.length];
        System.arraycopy(staticLocations, 0, locations, 0, staticLocations.length);
        System.arraycopy(SERVLET_LOCATIONS, 0, locations, staticLocations.length, SERVLET_LOCATIONS.length);
        return locations;
    }

SERVLET_LOCATIONS赋值为:

private static final String[] SERVLET_LOCATIONS = new String[]{"/"};

综上所述 springboot 的静态资源位置有5个
分别为:
1.classpath:/META-INF/resources/
2.classpath:/resources/
3.classpath:/static/
4.classpath:/public/
5./
一般情况下springboot项目没有webapp目录,所以5./路径一般不考虑

我们可以发现其中并没有classpath:templates.

二、如何能让我们放在templates文件夹底下的静态资源被访问到呢。
不用不得了,已经有那么多静态资源目录,何必非要跟模板争地方

三、如果静态资源过滤策略不能满足开发要求,也可以自定义静态资源过滤策略,自定义策略有两种方式:
①application.properties配置文件中添加配置

spring.mvc.static-path-pattern=/static/**
spring.resources.static-location=/staitc/

过滤规则为:/static/**,静态资源位置为/static/
而这样设置,在浏览器中输入http://localhost:8080/static/123.png,才能访问静态资源
②用java编码方式来定义 其中又分两种,
extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter
implements WebMvcConfigurer
这里推荐后一种,写法如下

@Configuration
public class MyWebMvcAutoConfig  implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
}

重启项目,输入localhost:8080/static/123.png即可看到classpath:/static/底下的静态文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值