【问题标题】:How to loop through a list of self referencing entity in Thymeleaf?如何遍历 Thymeleaf 中的自引用实体列表?
【发布时间】:2017-12-25 01:43:46
【问题描述】:
我有一个实体如下
public class Entity{
....
private List<Entity> entities = new ArrayList<>();
....
//Other fields with its setters and getters
}
如何使用 Thymeleaf 显示所有 entities。我刚刚开始使用 thymeleaf,并尝试在可能的地方进行搜索。
【问题讨论】:
标签:
java
spring-boot
thymeleaf
【解决方案1】:
我会从 java 端接近它。例如这样的事情:
JAVA
public class Entity{
private List<Entity> entities = new ArrayList<>();
public List<Entity> getAllEntities() {
List<Entity> all = new ArrayList<>();
for (Entity e: entities) {
all.add(this);
all.addAll(e.getAllEntities())
}
return all;
}
}
HTML
<div th:each="entity: ${entities.allEntities}">
<span th:text="${entity.someProperty}" />
</div>
也可以通过包含一个片段来实现——但我不确定百里香叶片段是否可以包含自身。