方便我们后期的展示
@Data
public class User {
String username;
int age;
int sex;
boolean isVip;
List<String> tags;
Date date;
}
@GetMapping("/p3")
public void entity(Model model) {
User user = new User();
user.setUsername("Look");
user.setAge(18);
user.setSex(1);
user.setVip(true);
user.setTags(List.of("PHP", "Java", "Python"));
model.addAttribute("user",user);
}
th:text
和${uesr.username}
展示用户名称th:switch
进行多条件的判断th:if
进行条件的判断th:each
进行对象的遍历<!DOCTYPE html>
<html lang="en" xmlns:th="<http://www.thymeleaf.org>">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${user.username}"></h1>
<h2 th:text="${user.age}"></h2>
<div th:switch="${user.sex}">
<p th:case="1">男</p>
<p th:case="0">女</p>
<p th:case="*">未知</p>
</div>
<div th:if="${user.isVip}">你是尊贵的Vip</div>
<ul>
<li th:each="tag:${user.tags}" th:text="${tag}"></li>
</ul>
</body>
</html>