Spring Boot: 集成 Thymeleaf 模板
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
添加配置信息
spring:
thymeleaf:
mode: HTML
encoding: utf-8
cache: false
check-template: false
check-template-location: false
新增html页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<td>编号</td>
<td>用户名</td>
<td>电话</td>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.phone}"></td>
</tr>
</table>
</body>
</html>
编写Controller
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserDao userDao;
@GetMapping("/index")
public String index(Model model) {
List<UserDO> users = userDao.selectList(new QueryWrapper<UserDO>());
model.addAttribute("users", users);
return "system/user/demo";
}
}
运行项目
- 浏览器访问 http://localhost:port/user/index
![在这里插入图片描述](https://img-blog.csdnimg.cn/7f051f26fd26414e9b79adf54cc497fc.png#pic_center)