首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >评论树,回复回复显示

评论树,回复回复显示
EN

Stack Overflow用户
提问于 2019-10-18 02:16:05
回答 1查看 380关注 0票数 0

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

Comment.java

代码语言:javascript
复制
...
    @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

代码语言:javascript
复制
model.addAttribute("comments", commentRepository.findAllCommentsByPostIdAndParentIsNull(id));

post.html

代码语言:javascript
复制
<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>

评论数据库

代码语言:javascript
复制
ID | CONTENT | PARENT_ID | POST_ID
1    text1         null        1
2    text2          1          1
3    text3          2          1
4    text4         null        1

我想要的输出

代码语言:javascript
复制
text1
  text2
    text3
text4

我得到的输出

代码语言:javascript
复制
text1
 text2
text4

基本上,回复不会显示出来。

如何获得我想要的输出?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-18 03:16:14

请尝试以下内容

代码语言:javascript
复制
<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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58438561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档