首页处理
由于首页还是与webMVC有关,我们还是从spring-boot-autoconfigure依赖的METAINF下的spring.factories下面的WebMvcAutoConfiguration类中看源码查找
在WebMvcAutoConfiguration类中发现有一个方法欢迎页处理映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
// getWelcomePage 获得欢迎页面
this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
return welcomePageHandlerMapping;
}
Ctrl + 鼠标左键点入方法getWelcomePage()获取欢迎页
它还是会从静态资源路径下去寻找"classpath:/META-INF/resources/",“classpath:/resources/”, “classpath:/static/”, “classpath:/public/”,它调用getIndexHtml()方法去匹配有没有index.html这个文件,如果静态资源中存在index.html而且请求路径不为空则获取到了欢迎页面
private Resource getWelcomePage() {
//从静态资源路径下去寻找"classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/"
for (String location : this.resourceProperties.getStaticLocations()) {
Resource indexHtml = getIndexHtml(location);
if (indexHtml != null) {
return indexHtml;
}
}
ServletContext servletContext = getServletContext();
if (servletContext != null) {
return getIndexHtml(new ServletContextResource(servletContext, SERVLET_LOCATION));
}
return null;
}
//通过类加载器加载资源
private Resource getIndexHtml(String location) {
return getIndexHtml(this.resourceLoader.getResource(location));
}
private Resource getIndexHtml(Resource location) {
//调用getIndexHtml()方法去匹配有没有index.html这个文件
try {
Resource resource = location.createRelative("index.html");
if (resource.exists() && (resource.getURL() != null)) {
return resource;
}
}
catch (Exception ex) {
}
return null;
}
欢迎页面:静态资源文件夹下的所有 index.html 页面会被 / 映射,会自动从静态资源文件夹下的找index.html,springboot已经帮我们拼接好了4个静态资源路径
当我们在任意四个静态资源的路径下建立index.html时,浏览器访问都能访问成功
但如果四个默认静态资源路径下都存在index.html文件时,还是会有优先级顺序,即
“classpath:META-INF/resources/”>“classpath:/resources/”>“classpath:/static/”>“classpath:/public/”
关于网址图标设置
与其他静态资源一样,Spring Boot在配置的静态内容位置中查找 favicon.ico。如果存在这样的文件,它将自动用作应用程序的favicon
- 关闭SpringBoot默认图标
#关闭默认图标
spring.mvc.favicon.enabled=false
-
自己放一个图标在静态资源目录下,四个静态资源路径的目录下均可
-
清除浏览器缓存!刷新网页,发现图标已经变成自己的了