2014-07-07 38 views
8

我是新來的百里香葉,想用一個數組和一個循環做一個簡單的表。在百里香葉中創建一張桌子

我的代碼如下所示:

<!DOCTYPE HTML> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head> 
<title>Smoke Tests</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head> 
<body> 
<table border="1" style="width:300px"> 
<tr> 
<td>Test Name</td> 
</tr> 
<tr th:each="smokeTest : ${smokeTests}"> 
<td> 
    th:text="${smokeTest.name}">A Smoke Test' 
</td> 
</tr> 
</table> 
</body> 
</html> 

基本上我的問題是,我不能在<tr>的run循環爲<td>秒。有什麼方法可以使這個代碼工作?

這是第一位想到

回答

5

簡單的解決方案:

<th:block th:each="smokeTest : ${smokeTests}"> 
    <tr> 
     <td th:text="${smokeTest.name}">A Smoke Test'</td> 
    </tr> 
</th:block> 

詳情:http://www.thymeleaf.org/whatsnew21.html#bloc

+0

這不起作用becase th:文本沒有關係的循環。 – user3073234

+0

我剛更新了我的答案。 –

5

你必須把日:文字作爲標籤的屬性,所以

<tr th:each="smokeTest : ${smokeTests}"> 
    <td th:text="${smokeTest.name}">A Smoke Test'</td> 
</tr> 

應該運行。