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");
}