SpringBoot学习笔记:整合Thymeleaf
Posted 听风者-better
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot学习笔记:整合Thymeleaf相关的知识,希望对你有一定的参考价值。
1. 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.添加配置
#关闭页面缓存
spring.thymeleaf.cache=false
3.具体代码与页面
- controller层
@Controller
@RequestMapping(value = "/user")
public class UserController
@GetMapping(value = "/one")
public String getUser(Model model)
User user = new User(1, "唐万言", "123456", new Date());
model.addAttribute("user", user);
return "one";
@GetMapping(value = "/list")
public String getUserList(Model model)
User u1 = new User(1, "唐万言", "123456", new Date());
User u2 = new User(2, "王心心", "123456", new Date());
List<User> list = new ArrayList<>();
list.add(u1);
list.add(u2);
model.addAttribute("list", list);
return "list";
- 页面
html页面使用thymeleaf标签需要在页面标签先引入:
<html xmlns:th="http://www.thymeleaf.org">
one.html
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="$user.id"></p>
<p th:text="$user.username"></p>
<p th:text="$user.password"></p>
<p th:text="$user.birthday"></p>
</body>
</html>
list.html
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:each="user : $list">
<p th:text="$user.id"></p>
<p th:text="$user.username"></p>
<p th:text="$user.password"></p>
<p th:text="$#dates.format(user.birthday,'yyyy-MM-dd')"></p>
</div>
</body>
</html>
- 效果
4.thymeleaf常用语法
以上是关于SpringBoot学习笔记:整合Thymeleaf的主要内容,如果未能解决你的问题,请参考以下文章