在ThymeLeaf中循环遍历数组

时间:2014-06-27 20:02:06

标签: arrays loops thymeleaf paragraph

我是ThymeLeaf的新手,我想知道是否有办法循环<p> html标记以及遍历<p>标记内的数组。我希望smokeTest中的元素最终出现在不同的段落中。

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p th:text="${smokeTests[0].name}"/>
</body>
</html>

感谢您的帮助

2 个答案:

答案 0 :(得分:41)

您是否尝试过以下代码?我没有测试它,因为它经常被使用:

<body>
    <p th:each="smokeTest : ${smokeTests}"
       th:text="${smokeTest.name}">A Smoke Test</p>
</body>

答案 1 :(得分:2)

这很直截了当。你可以这样做:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:each="smokeTest : ${smokeTests}" th:text="${smokeTest.name}"><p/>
</body>
</html>

您还可以使用段落标记以外的其他一些html标记。像这样:

<h2 th:each="smokeTest : ${smokeTests}" th:text="${smokeTest.name}"><h2/>