我需要实现评论树,但无法得到回复回复工作。
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.java
model.addAttribute("comments", commentRepository.findAllCommentsByPostIdAndParentIsNull(id));post.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_ID
1 text1 null 1
2 text2 1 1
3 text3 2 1
4 text4 null 1我想要的输出
text1
text2
text3
text4我得到的输出
text1
text2
text4基本上,回复不会显示出来。
如何获得我想要的输出?
发布于 2019-10-18 03:16:14
请尝试以下内容
<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>https://stackoverflow.com/questions/58438561
复制相似问题