一、添加Thymeleaf依赖

<!--Thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

二、在application.yml文件中进行Thymeleaf相关配置

spring:
  #thymeleaf模板配置
  thymeleaf:
    #开发配置为false,避免修改模板还要重启服务器
    cache: false
    #模板的模式,支持HTML,XML,TEXT,JAVASCRIPT
    mode: HTML5
    #编码
    encoding: utf-8
    #内容类别
    servlet:
      content-type: text/html
    #配置模板路径,默认是templates,可以不用配置
    prefix: classpath:/templates

三、通过接口,指定初始化接口。要注意接口中页面路径的写法

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 项目初始化相关配置
 * 例如:
 * 1、首页
 */
@RestController
public class ProjectCotroller {

    @GetMapping("/")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/index");
        return modelAndView;
    }

}

 

四、在templates目录下书写首页

SpringBoot整合Thymeleaf_java