SpringBoot—static-path-pattern与static-location

springboot处理请求的时候,首先是将请求映射去所有controller匹配,如果匹配不到对应的映射,则统一从静态资源查找,如果静态资源也没有匹配到则报404。
默认静态资源的位置(底层源码)
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", 
"classpath:/resources/", "classpath:/static/", "classpath:/public/"};
修改静态资源默认位置

注意:修改后,默认静态资源位置全部失效~

spring:
 web:
 resources:
 static-locations:  classpath:/haha/
修改静态资源默认访问路径
spring:
 mvc:
 static-path-pattern: res/**

注意:修改了默认访问路径,会使欢迎页面失效!(底层源码)
如果使用到模板引擎(templates下的index.html会作为欢迎界面)

 WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders, ApplicationContext applicationContext, Resource welcomePage, String staticPathPattern) {
        if (welcomePage != null && "/**".equals(staticPathPattern)) {
            logger.info("Adding welcome page: " + welcomePage);
            this.setRootViewName("forward:index.html");
        } else if (this.welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
            logger.info("Adding welcome page template: index");
            this.setRootViewName("index");
        }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值