评论树,回复回复即可显示

我需要实现评论树,但无法获得回复工作的回复。


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));

帖子.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

基本上回复的回复都不会显示。


我该如何获得我想要的输出?


忽然笑
浏览 55回答 1
1回答

撒科打诨

尝试以下方法<th:block th:each="comment : ${comments}">&nbsp; <p th:text="${comment.content}">comment</p>&nbsp; <div th:fragment="f_call(comment)"&nbsp;&nbsp; &nbsp; &nbsp; th:unless="${#lists.isEmpty(comment.children)}" >&nbsp; &nbsp; &nbsp; <div th:each="child : ${comment.children}" th:inline="text">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [[${child.content}]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div th:replace="this::f_call(${child})"></div>&nbsp; &nbsp; &nbsp; </div>&nbsp; </div></th:block>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java