SpringBoot学习日志之DAY05springboot默认首页设置

48 篇文章 0 订阅
38 篇文章 2 订阅

目录

什么是默认首页

配置springboot默认首页

方式一:通过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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值