在springboot整合视图层,官方推荐使用thymeleaf。
thymeleaf只是渲染html的一种方式,是一种模板。
第一步创建一个maven项目

第二步:修改Jdk版本,添加thymeleaf启动器
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
第三步:在resources下创建templates目录

第四步:创建controller层
package com.spring;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MessageController {
@RequestMapping("/showMessage")
public String showMessage(Model model) {
model.addAttribute("msg", "thymeleaf");
return "/index";
}
}
第五步:在templates目录下创建html页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<span th:text="hello,我是thymeleaf"></span>
<hr/>
<span th:text="${msg}"></span>
</body>
</html>
第六步:创建启动类进行启动,访问controller
如果我们没有指定thymeleaf的版本就会出现异常,因为在thymeleaf3.0以前,对html有严格的规定,所有的标签都必须闭合,否则就会出现异常

解决方案:1添加闭合标记
2.修改thymeleaf和thymeleaf-layout-dialect的版本
<properties>
<java.version>1.7</java.version>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>