目录
方式一:通过controller指定RequestMapping路径为(/)
方式二:通过自定义配置类实现WebMvcConfigurer进行设置
方式三:直接将index.html页面放在templates下面就行了
方式四 直接将index.html文件放在static目录下面
什么是默认首页
在启动WEB项目之后,在浏览器输入:127.0.0.1:8080 会自动进入的界面就是默认首页
在springMvc当中,直接在web.xml当中进行如下配置就会将webapp下的index.html当成默认首页
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
配置springboot默认首页
在springboot当中不存在web.xml,那么怎么配置默认首页呢?
在网上搜索了很多的资料,发现两种方式:备注:以下所有方式都是使用的thymeleaf前端模板并没有使用jsp
#thymeleaf 配置信息
#取消缓存,修改HTML界面后立即生效,不然会读取缓存,没有变化
spring.thymeleaf.cache=false
#下面5个配置信息都是常用的默认的配置信息,如果不需要修改,不写也行
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.servlet.content-type=text/html
方式一:通过controller指定RequestMapping路径为(/)
@Controller
public class IndexController {
@RequestMapping("/")
public String index(){
return "test/index";
}
}
方式二:通过自定义配置类实现WebMvcConfigurer进行设置
@Configuration
public class IndexView implements WebMvcConfigurer{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/test/index");
}
}
方式三:直接将index.html页面放在templates下面就行了
正常Springboot启动信息如下:
在templates下面放入index.html文件后的启动信息如下:
在打印信息当中可以看到自动添加欢迎界面index
具体原因是因为在:
WebMvcAutoConfiguration这个类中进行了相关的映射
方式四 直接将index.html文件放在static目录下面
springboot默认static目录下面的是静态资源,会优先加载static下面的index.html。
参考资料:https://blog.csdn.net/fmwind/article/details/81235401