Spring Boot欢迎页
spring boot欢迎页,就是默认访问的页面,默认为Resources类的CLASSPATH_RESOURCE_LOCATIONS属性,spring boot通过EnableWebMvcConfiguration类的getWelcomePage方法获取欢迎页的路径
1
| private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| private Resource getWelcomePage() { String[] var1 = this.resourceProperties.getStaticLocations(); int var2 = var1.length;
for(int var3 = 0; var3 < var2; ++var3) { String location = var1[var3]; Resource indexHtml = this.getIndexHtml(location); if (indexHtml != null) { return indexHtml; } }
ServletContext servletContext = this.getServletContext(); if (servletContext != null) { return this.getIndexHtml((Resource)(new ServletContextResource(servletContext, "/"))); } else { return null; } }
|
方法中的resourceProperties.getStaticLocations(),获取其实就是spring.web.resources.static-locations的值,在没有配置的情况下,spring.web.resources.static-locations为上面CLASSPATH_RESOURCE_LOCATIONS的值,具体可查看resourceProperties类源码。
org.springframework.web.servlet.config.annotation.WebMvcConfigurer 是Spring Web MVC框架中的一个接口,用于自定义和扩展Web MVC的配置。开发者可以通过实现这个接口来修改或增强Spring MVC的行为,包括一下功能
- addFormatters:添加格式化程序
- addResourceHandlers:对特定路径配置静态资源映射的方法,这里只能配置到文件夹
- addViewControllers:用于将特定的URL请求映射到特定的视图(需要注意视图和资源的区别),而不需要再创建Controller
- addInterceptors:对特定路径的URL配置过滤器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @Component public class WebMvcConfig implements WebMvcConfigurer {
@Override public void addFormatters(FormatterRegistry registry) { registry.addFormatter(new DateFormatter("yyyy-MM-dd")); }
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/WebContent").addResourceLocations("file:D:\\Order\\WebContent"); }
@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/testh").setViewName("testh"); }
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new UserRoleAuthorizationInterceptor()).addPathPatterns("/user"); } }
|