创建实体对象

方便我们后期的展示

@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);
}

基础标签使用

<!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>