SpringBoot的index首页的访问、自定义Favicon图标
目录
- 1. index首页
- 1.1 index首页访问规则的源码
- 1.2 index首页的访问
- 2. 自定义Favicon图标
1. index首页
1.1 index首页访问规则的源码
package org.springframework.boot.autoconfigure.web.servlet;
......省略部分......// SpringBoot给容器中放WebMvcConfigurationSupport组件// 我们如果自己放了WebMvcConfigurationSupport组件,SpringBoot的WebMvcAutoConfiguration都会失效@Configuration(proxyBeanMethods = false)@EnableConfigurationProperties({WebProperties.class})public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {......省略部分......@Beanpublic WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {return (WelcomePageHandlerMapping)this.createWelcomePageHandlerMapping(applicationContext, mvcConversionService, mvcResourceUrlProvider, WelcomePageHandlerMapping::new);}......省略部分......}
......省略部分......
WelcomePageHandlerMapping:
- 访问/**路径下的所有请求,都在以前四个静态资源路径下找,欢迎页也一样
- 找index.html:只要静态资源的位置有一个index.html页面,项目启动默认访问
1.2 index首页的访问
可以在静态资源目录下放index.html文件,就能访问index首页。如resources\META-INF\resources\index.html的文件内容如下:
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>test title</title>
</head>
<body><h1>hello springboot</h1></body>
</html>
然后访问http://localhost:8080/,效果如下:
注意:配置文件不要配置spring.mvc.static-path-pattern参数,否则不能访问index首页
另一种方法:也可以通过Controller控制器,对请求进行处理,跳转到index首页
2. 自定义Favicon图标
将favicon.ico文件放到静态资源目录下,然后访问任意一个URL,就会显示小图标。如果没显示小图标,注意清一下浏览器缓存
注意:配置文件不要配置spring.mvc.static-path-pattern参数,否则不能看到Favicon图标
例如,访问http://localhost:8080/,效果如下所示: