我需要實現評論樹,但無法獲得回復工作的回復。Comment.java... @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; @ManyToOne private Post post; @ManyToOne private Comment parent; @OneToMany(mappedBy = "parent") private List<Comment> children = new ArrayList<Comment>();...PostController.javamodel.addAttribute("comments", commentRepository.findAllCommentsByPostIdAndParentIsNull(id));帖子.html<th:block th:each="comment : ${comments}"><p th:text="${comment.content}">comment</p><th:block th:each="child : ${comment.children}"><div class="child"> <p th:text="${child.content}">comment</p></div></th:block></th:block>評論數據庫ID | CONTENT | PARENT_ID | POST_ID1 text1 null 12 text2 1 13 text3 2 14 text4 null 1我想要的輸出text1 text2 text3text4我得到的輸出text1 text2text4基本上回復的回復都不會顯示。我該如何獲得我想要的輸出?
1 回答

撒科打諢
TA貢獻1934條經驗 獲得超2個贊
嘗試以下方法
<th:block th:each="comment : ${comments}">
<p th:text="${comment.content}">comment</p>
<div th:fragment="f_call(comment)"
th:unless="${#lists.isEmpty(comment.children)}" >
<div th:each="child : ${comment.children}" th:inline="text">
[[${child.content}]]
<div th:replace="this::f_call(${child})"></div>
</div>
</div>
</th:block>
添加回答
舉報
0/150
提交
取消