一、Spring Boot整合Thymeleaf
1、创建Spring Boot项目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/41172485b5fd356686b31dd441030ad2.png)
- 热部署
- 网页
- 模板引擎
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f63f09e3e4c9484a7ed66b7883d6c853.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/514e5c88fc57b42ae9435069350c7dcf.png)
2、在全局配置文件里配置Thymeleaf属性
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0602bbbe6f937b91a3b436de898f1858.png)
#缓存配置,默认即是true,开发阶段设置为false
spring.thymeleaf.cache = false
#设置模板使用的编码为utf-8
spring.thymeleaf.encoding = UTF-8
#指定为模板使用的模式为html5,默认html
spring.thymeleaf.mode = HTML5
#指定前缀,默认位置为/templates/,可以修改成其它位置
spring.thymeleaf.prefix = classpath:/templates/
#指定模板文件后缀,默认值为.html,可以修改成其它值
spring.thymeleaf.suffix = .html
3、创建登录控制器LoginController
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dbeeef797c4b3a611295e0d6003b4789.png)
package net.zy.lesson09.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Calendar;
@Controller
public class LoginController {
@GetMapping("toLoginPage")
public String toLoginPage(Model model){
model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
return "login";
}
}
4、创建模板文件,获取控制器传来的动态数据
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/27cbb88c02c37be2984debb286deb374.png)
<!DOCTYPE html>
<html lang="en" xmlns:th="html://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<span th:text="${currentYear}">今年</span> -
<span th:text="${currentYear} + 1">明年</span>
</body>
</html>