java - 嵌套迭代 thymeleaf

标签 java spring-boot thymeleaf

我想迭代嵌套的 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>

最佳答案

我认为您的 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)。

关于java - 嵌套迭代 thymeleaf ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56041761/

相关文章:

spring-boot - Spring Boot Gradle bootRun 任务默认类路径和当前工作目录

html - 如何使用 thymeleaf 将参数传递给模态?

java - Spring MVC 和 Thymeleaf - 之间有什么区别 :value and th:field when you iterate multiple items?

java - 如何从操作类中选择特定对象

java - 复制到 SD 的 boolean 问题,需要一些帮助来更正

spring - 如何在访问部署在 Tomcat 上的 restful spring-boot 应用程序时修复此 IlleagalstateException?

java - 如何从 spring boot 项目中获取当前的 thymeleaf 版本?

java - 如何使用 selenium 2.53.0 在 geckodriver 上使用最新的 Firefox 浏览器运行 selenium 脚本?

java - 如何通过 JRuby 将 Java 字符数组作为参数正确传递给 Ruby 脚本?

java - 如何使用 Spring boot 返回图像