【问题标题】:Loop through array in ThymeLeaf遍历百里香叶中的数组
【发布时间】:2014-08-19 01:01:21
【问题描述】:
我是 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>
感谢您的帮助
【问题讨论】:
标签:
arrays
loops
thymeleaf
paragraph
【解决方案1】:
您是否尝试过以下代码?我没有测试它,因为它经常被使用:
<body>
<p th:each="smokeTest : ${smokeTests}"
th:text="${smokeTest.name}">A Smoke Test</p>
</body>
【解决方案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/>