在ThymeLeaf中循环遍历数组

28

我是ThymeLeaf新手,想知道是否有一种方法可以循环遍历 <p> HTML标签,并在该标签内迭代一个数组。 我希望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个回答

49

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

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

3
这很简单。你可以这样做:
<!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/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接