我需要实现评论树,但无法获得回复工作的回复。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
提交
取消