【问题标题】:Iterate with index using thymeleaf使用 thymeleaf 迭代索引
【发布时间】:2014-01-23 13:14:48
【问题描述】:

我是 thymeleaf 的新手,正在将我所有的 jsp 代码转换为 thymeleaf。我不知道如何将下面的代码转换为 thymeleaf。有人知道如何将下面的代码转换为 thymeleaf 吗?

<logic:iterate id="id" property="idList" name="sampleForm" indexId="i">
    <label for="id<%=i%>">
      <bean:write name="id" property="id" />
    </label>
</logic:iterate>

请告诉我如何初始化thymeleaf 中的索引值以用于某些值??

【问题讨论】:

    标签: html spring jsp thymeleaf


    【解决方案1】:
    <label th:each="id,status : ${idList}" th:for="|id${status.index}|" th:text="${id.id}"></label>
    
    • th:each 将遍历idList,将每个项目分配给id,并为每个项目创建一个label。可以通过添加一个额外的名称来分配项目的状态,用逗号分隔(在此示例中为status)。
    • th:for 将设置标签的for 属性。管道 (|) 用于简单的字符串连接。
    • th:text 会将标签的内部文本设置为 ID。

    【讨论】:

      【解决方案2】:

      你也可以这样使用:

      <label th:each="id : ${idList}" th:for="${'id' + idStat.index}" th:text="{id.id}">
      

      这会从 0 开始索引

      如果你想从 1 开始索引使用这个

      <label th:each="id : ${idList}" th:for="${'id' + idStat.count}" th:text="{id.id}">
      

      查看Thymeleaf documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-11
        • 1970-01-01
        • 2019-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-14
        相关资源
        最近更新 更多