请教大家一个thymeleaf的一个问题。
后端准备了3个List
thymeleaf如何同时遍历这个3个列表,以表格的形式展现出来?
<tr th:each="address, name, license : ${teaAddrList}, ${teaNameList}, ${teaLicenseList}">
<td th:text="${address}"></td>
<td th:text="${name}"></td>
<td th:text="${license}"></td>
</tr>
我这个:会报错,恳请大家的帮助~
后端组装好,返回一个集合对象不行么,对象包含三个属性address, name, license
【以下回答由 GPT 生成】
对于这个问题,可以使用th:with语句来解决。首先,需要将三个列表绑定到一个新的变量上,然后再在th:each中使用该变量。以下是一个解决方案:
<tr th:each="i : ${#numbers.sequence(0, #lists.size(teaAddrList)-1)}">
<td th:text="${teaAddrList[i]}"></td>
<td th:text="${teaNameList[i]}"></td>
<td th:text="${teaLicenseList[i]}"></td>
</tr>
首先,使用#numbers.sequence(0, #lists.size(teaAddrList)-1)
来生成一个由0到teaAddrList
长度-1的数字序列。然后使用th:each="i : ${...}"
来遍历该数字序列。
在每次遍历中,使用${teaAddrList[i]}
、${teaNameList[i]}
和${teaLicenseList[i]}
来获取对应索引位置的值。
由于teaAddrList、teaNameList和teaLicenseList的长度应该一致,所以以任何一个列表的长度来生成数字序列都可以。这样,三个列表中的元素就会按顺序一一对应地展示在表格中。
希望这个解决方案能够帮助到你!如果你还有任何问题,请随时提问。
【相关推荐】