【问题标题】:For Loops in ThymeLeaf ViewsThymeLeaf 视图中的 For 循环
【发布时间】:2018-03-27 18:41:02
【问题描述】:

我希望在我的 ThymeLeaf 视图中使用 for 循环。

我有一个包含 8 个对象的列表模型。

我想在一个区域循环对象从 1 到 4,在另一个区域循环对象从 5 到 8。

这个可以吗?

有没有一种方法可以操作 for each 循环以适合我的应用程序?

提前致谢!

【问题讨论】:

    标签: for-loop spring-boot web model-view-controller thymeleaf


    【解决方案1】:

    看这里http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status

    您可以使用th:each 访问实际的索引和计数变量 从那里您可以读取第一个块中的值并在计数为 4 时停止。在第二个块中您可以添加另一个 th:each 再次迭代,但只能从计数 5 开始。

    【讨论】:

      【解决方案2】:

      在这种情况下,您可能应该使用#numbers 实用程序对象。像这样的东西,例如:

      <span th:each="n: ${#numbers.sequence(1, 4)}" th:text="${n}" />
      <span th:each="n: ${#numbers.sequence(5, 8)}" th:text="${n}" />
      

      (这两个块将分别打印 1 到 4 和 5 到 8。)您的列表(如果命名为列表)将如下所示:

      <span th:each="n: ${#numbers.sequence(1, 4)}" th:text="${list[n].fieldOne}" />
      <span th:each="n: ${#numbers.sequence(5, 8)}" th:text="${list[n].fieldTwo}" />
      

      【讨论】:

      • 谢谢。这正是我想要的!
      猜你喜欢
      • 2017-09-20
      • 1970-01-01
      • 2016-01-01
      • 2018-06-04
      • 1970-01-01
      • 2011-10-18
      • 2014-03-29
      • 2023-03-26
      相关资源
      最近更新 更多