Dynamic 素数动态树节点

Dynamic 素数动态树节点,dynamic,primefaces,treenode,Dynamic,Primefaces,Treenode,我正试图建立一个动态树节点来实现像Reddit这样的评论系统。如果您不熟悉,请参考以下内容: 现在数据库中有一个注释表,如下所示 id parent content 1 0 text1 2 0 text2 3 1 child of text 1 4 3 child to the child of text 1 - text1 - child of text1 - child to the child of text 1 - text2

我正试图建立一个动态树节点来实现像Reddit这样的评论系统。如果您不熟悉,请参考以下内容:

现在数据库中有一个注释表,如下所示

id parent content
1 0       text1
2 0       text2
3 1       child of text 1
4 3       child to the child of text 1
- text1
  - child of text1
    - child to the child of text 1
- text2
因此,评论面板将如下所示

id parent content
1 0       text1
2 0       text2
3 1       child of text 1
4 3       child to the child of text 1
- text1
  - child of text1
    - child to the child of text 1
- text2
我无法想象如何将这个表设计与Primefaces Treenode Java代码结合起来,如果有人能给我指出正确的方向,那就太好了。我尝试过在论坛和谷歌上搜索,但找不到一个我能理解的解决方案

我可能应该提到,因为这是用户生成的内容,所以我的表中没有固定的结构,因此后端代码应该能够动态地处理它

我感谢你的帮助

谢谢,
bhoen

我用递归方法和上表结构解决了这个问题:

public TreeNode createDocuments() {
    TreeNode rootNode = new DefaultTreeNode(new Document(), null);
    List<Document> documentRootNodeList = dao.getDocumentsRoot();
    for (Document doc : documentRootNodeList) {
        TreeNode node = new DefaultTreeNode(doc, rootNode);
        createSubNode(doc, node);
    }
    return rootNode;
}

public void createSubNode(Document doc, TreeNode node) {
    List<Document> documentList = dao.getDocumentsNode(doc);
    for (Document subDoc : documentList) {
        TreeNode subNode = new DefaultTreeNode(subDoc, node);
        createSubNode(subDoc, subNode);
    }
}
public树节点createDocuments(){
TreeNode rootNode=new DefaultTreeNode(new Document(),null);
List documentRootNodeList=dao.getDocumentsRoot();
for(文档文档文档:documentRootNodeList){
TreeNode=新的默认TreeNode(文档,根节点);
创建子节点(文档、节点);
}
返回根节点;
}
public void createSubNode(文档文档,树节点){
List documentList=dao.getDocumentsNode(doc);
用于(文档子文档:文档列表){
TreeNode子节点=新的默认TreeNode(子文档,节点);
创建子节点(子文档,子节点);
}
}

无论如何,我最终通过这样做解决了自己的问题。Map treeNodeMap=newhashmap();for(Comment Comment:comments){TreeNode node=null;//表示顶级注释,如果(Comment.getParent()=0){node=new DefaultTreeNode(Comment.getContent(),root);}否则{node=new DefaultTreeNode(Comment.getContent()),treeNodeMap.get(comment.getParent());}treeNodeMap.put(comment.getId(),node);}将其作为答案而不是注释发布。此项目,因为版本2.1旨在实现您在问题中描述的功能。