2014-01-06 64 views
11

我是thymeleaf的新手,並且將我所有的jsp代碼轉換爲thymeleaf.I不知道如何將下面的代碼轉換爲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指數值在某些值中使用?

回答

15
<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。
1

您也可以使用這樣的:

<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