考虑以下List
中的List
:
List<List<SubSection>> listOfSubSectionLists
我在Thymeleaf 3.0.9 TEXT模板中需要两个对应的循环嵌套,如下所示:
模板:
[# th:each="ssList, iterStat : ${listOfSubSectionLists}"]
Title: Sub-Section [( ${ssList.iterStat.index+1} )]
[# th:each="subSection : ${ssList.**?**}"]
Name: [( ${subSection.name} )]
Address: [( ${subSection.address} )]
Phone: [( ${subSection.phone} )]
[/]
[/]
是否可以在上面的模板的第三行中获取内部列表?
最佳答案
如果您的变量如前所述,则列表仅为ssList
。因此,代码应为:
[# th:each="ssList, iterStat: ${listOfSubSectionLists}"]
Title: Sub-Section [( ${iterStat.count} )]
[# th:each="subSection: ${ssList}"]
Name: [( ${subSection.name} )]
Address: [( ${subSection.address} )]
Phone: [( ${subSection.phone} )]
[/]
[/]
一些注意事项:
iterStat
是它自己的变量(与ssList无关)。您不能使用表达式ssList.iterStat
访问它。iterStat.count
等效于iterStat.index + 1