使用Thymeleaf循环遍历

Thymeleaf是一种Java服务器模板引擎,可用于构建Web应用程序。它允许我们在HTML模板中嵌入表达式,使得展示数据变得更加简单。在本文中,我们将重点介绍如何使用Thymeleaf循环遍历

1. 循环遍历列表

循环遍历列表是使用Thymeleaf的常见操作。假设我们有以下Java类:

public class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 省略 getter 和 setter
}

我们可以在Controller中将一组User对象存储在Model中:

@GetMapping("/users")
public String getUsers(Model model) {
    List<User> users = new ArrayList<>();
    users.add(new User("张三", 20));
    users.add(new User("李四", 25));
    users.add(new User("王五", 30));
    model.addAttribute("users", users);
    return "users";
}

在上面的代码中,我们将一个名为"users"的List<User>对象存储在Model中,然后返回一个名为"users"的HTML模板。

接下来,在HTML模板中使用Thymeleaf循环遍历这个列表:

<table>
    <thead>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="user : ${users}">
            <td th:text="${user.name}"></td>
            <td th:text="${user.age}"></td>
        </tr>
    </tbody>
</table>

在上面的代码中,我们使用了th:each属性来循环遍历"users"列表,"user"是循环变量,"${users}"是要遍历的列表。在每次循环中,我们使用th:text属性将每个User对象的"name"和"age"属性值显示在HTML表格中。

使用Thymeleaf循环遍历

2. 循环遍历Map

Thymeleaf也支持循环遍历Map。假设我们有以下的Java类:

public class City {
    private String name;
    private String province;

    public City(String name, String province) {
        this.name = name;
        this.province = province;
    }

    // 省略 getter 和 setter
}

我们可以在Controller中将一个Map<String, City>对象存储在Model中:

@GetMapping("/cities")
public String getCities(Model model) {
    Map<String, City> cities = new HashMap<>();
    cities.put("北京", new City("北京", "北京"));
    cities.put("上海", new City("上海", "上海"));
    cities.put("广州", new City("广州", "广东"));
    model.addAttribute("cities", cities);
    return "cities";
}

在上面的代码中,我们将一个名为"cities"的Map<String, City>对象存储在Model中,然后返回一个名为"cities"的HTML模板。

接下来,在HTML模板中使用Thymeleaf循环遍历这个Map:

<table>
    <thead>
        <tr>
            <th>城市</th>
            <th>省份</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="entry : ${cities}">
            <td th:text="${entry.key}"></td>
            <td th:text="${entry.value.province}"></td>
        </tr>
    </tbody>
</table>

在上面的代码中,我们使用了th:each属性来循环遍历"cities" Map,"entry"是循环变量,"${cities}"是要遍历的Map。在每次循环中,我们使用th:text属性将每个City对象的"name"和"province"属性值显示在HTML表格中。

3. 循环遍历数组

Thymeleaf也支持循环遍历数组。假设我们有以下的Java数组:

String[] fruits = {"苹果", "香蕉", "橙子"};
model.addAttribute("fruits", fruits);

在上面的代码中,我们将一个名为"fruits"的String数组存储在Model中。

接下来,在HTML模板中使用Thymeleaf循环遍历这个数组:

<ul>
    <li th:each="fruit : ${fruits}" th:text="${fruit}"></li>
</ul>

在上面的代码中,我们使用了th:each属性来循环遍历"fruits"数组,"fruit"是循环变量,"${fruits}"是要遍历的数组。在每次循环中,我们使用th:text属性将每个水果的名称显示在HTML列表中。

常见问答

1. Thymeleaf是什么?

Thymeleaf是一种Java服务器模板引擎,可用于构建Web应用程序。它允许我们在HTML模板中嵌入表达式,使得展示数据变得更加简单。

2. Thymeleaf支持哪些循环遍历操作?

Thymeleaf支持循环遍历列表、Map和数组。

3. 如何在Thymeleaf中循环遍历列表?

使用Thymeleaf循环遍历列表需要使用th:each属性。例如:

<tr th:each="user : ${users}">
    <td th:text="${user.name}"></td>
    <td th:text="${user.age}"></td>
</tr>

4. 如何在Thymeleaf中循环遍历Map?

使用Thymeleaf循环遍历Map需要使用th:each属性。例如:

<tr th:each="entry : ${cities}">
    <td th:text="${entry.key}"></td>
    <td th:text="${entry.value.province}"></td>
</tr>

5. 如何在Thymeleaf中循环遍历数组?

使用Thymeleaf循环遍历数组需要使用th:each属性。例如:

<li th:each="fruit : ${fruits}" th:text="${fruit}"></li>

6. 如何在Thymeleaf中使用循环变量?

在Thymeleaf中,我们可以使用th:each属性来定义循环变量。例如:

<tr th:each="user : ${users}">
    <td th:text

本文来源:词雅网

本文地址:https://www.ciyawang.com/yo27an.html

本文使用「 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 」许可协议授权,转载或使用请署名并注明出处。

相关推荐