【问题标题】: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,并尝试在可能的地方进行搜索。

【问题讨论】:

  • 你尝试了什么?
  • 尝试阅读百里香tutorial

标签: 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>

也可以通过包含一个片段来实现——但我不确定百里香叶片段是否可以包含自身。

【讨论】:

    猜你喜欢
    • 2021-04-02
    • 2021-12-09
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 2013-07-26
    • 2018-03-08
    相关资源
    最近更新 更多