首页 > 解决方案 > 嵌套迭代百里香叶

问题描述

我想迭代一个嵌套的 ArrayList 所以输出是这样的:

* 1
    * 1
    * 2
    * 3
    * 4
* 2
    * 1
    * 2
    * 3
    * 4
* 3
    * 1
    * 2
    * 3
    * 4

相反,thymeleaf 在第一次迭代中检索所有对象,我得到:

* 1
    * 1
    * 2
    * 3
    * 4
    * 1
    * 2
    * 3
    * 4
    * 1
    * 2
    * 3
    * 4
* 2

* 3

我的代码是:

    <ul th:each="row, row_iterator: ${theater.getRows()}">
        <li th:text="${row.getId()}"> </li>
            <ul th:each="seat, seat_iterator: ${row.getSeats()}">
                <li th:text="${seat.getId()}"> </li>
            </ul>
    </ul>

标签: javaspring-bootthymeleaf

解决方案


我认为您的 html 格式错误。这对你有用吗?

<ul>
    <li th:each="row: ${theater.rows}">
        <span th:text="${row.id}" />

        <ul>
            <li th:each="seat: ${row.seats}" th:text="${seatid}" />
        </ul>
    </li>
</ul>

一些注意事项:

  • 您可以通过使用 Javabean 格式约定来缩短所有内容(例如,row.getId()可以缩短为row.id)。

推荐阅读