使用递归从数据库构建primefaces树

时间:2012-09-13 05:12:26

标签: jsf jsf-2 primefaces

我正在使用primefaces树组件。我正面临着使用递归从数据库构造树的问题。我看了一下这个问题Primefaces tree from database。不过,我无法建造这棵树。 我在for循环中有一个getChildNodes列表,它被重复调用,因为它是递归函数。我无法解决它。以下是我的代码。

实体

package com.model;

import java.io.Serializable;
import javax.persistence.*;
import javax.persistence.Id;


/**
 * The persistent class for the tree database table.
 * 
 */
@Entity
@Table(name = "TREE")
public class Tree implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="NODE_ID")
    private int nodeId;

    @Column(name="NODE_DATA")
    private String nodeData;

    @Column(name="PARENT_ID")
    private int parentId;

    @Column(name="NODE_TYPE")
    private String nodeType;

    public Tree() {
    }

    public int getNodeId() {
        return this.nodeId;
    }

    public void setNodeId(int nodeId) {
        this.nodeId = nodeId;
    }

    public String getNodeData() {
        return this.nodeData;
    }

    public void setNodeData(String nodeData) {
        this.nodeData = nodeData;
    }

    public int getParentId() {
        return this.parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public String getNodeType() {
        return nodeType;
    }

    public void setNodeType(String nodeType) {
        this.nodeType = nodeType;
    }


}

ManagedBean

package com.managedBeans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.model.Tree;
import com.service.TreeService;

/**
 * @author ADMIN
 * 
 */

@Component("treeBeanRecursive")
@Scope("request")
public class TreeBeanRecursive {

    private TreeNode rootNode;

    @Autowired
    TreeService treeService;

    private List<Tree> childNodes;

    public TreeService getTreeService() {
        return treeService;
    }

    public void setTreeService(TreeService treeService) {
        this.treeService = treeService;
    }

    public TreeBeanRecursive() {

    }

    @PostConstruct
    public void init() {

        Tree root = treeService.getRoot();

        childNodes = treeService.getChildNodes();

        rootNode = getNodeWithChildren(root, null);

    }

    public TreeNode getNodeWithChildren(Tree t1, TreeNode parent){
        TreeNode newNode= new DefaultTreeNode(t1, parent);

        for (Tree tt : childNodes){
             TreeNode newNode2= getNodeWithChildren(tt, newNode);
        }

        return newNode;
   }



    public TreeNode getRootNode() {
        return rootNode;
    }

    public void setRootNode(TreeNode node) {
        rootNode = node;
    }

    public void showMessage() {
        treeService.showMessage();
    }

    public List<Tree> getChildNodes() {
        return childNodes;
    }

    public void setChildNodes(List<Tree> childNodes) {
        this.childNodes = childNodes;
    }

}

childNodes是除根节点之外的所有节点的列表。我从spring service treeService.getChildNodes();

获取列表

tree.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <link type="text/css" rel="stylesheet" href="themes/bluesky/skin.css" />
    <style type="text/css">
.ui-panel {
    font-size: 85% !important;
    font-family: calibri !important;
    align: "center" !important;
}

.customProjectNode {
    background-image: url(./../images/tree/root.png) !important;
    width: 17px;
    height: 16px;
}
</style>
</h:head>
<h:body>


    <h:form id="form">

        <p:tree value="#{treeBeanRecursive.rootNode}" var="node" id="tree">
            <p:treeNode>
                <h:outputText value="#{node.getNodeData()}" />
            </p:treeNode>
            <p:treeNode type="#{node.getNodeType()}" icon="customProjectNode">
                <h:outputText value="#{node.getNodeData()}" />
            </p:treeNode>
        </p:tree>


    </h:form>

</h:body>
</html>

我的表格包含以下数据。根节点父节点为0.

NodeID数据ParentID nodeType

'1','项目名称','0','项目名称'

'2','Node1','1','hardware'

'3','Node2','1','软件'

'4','Node3','1','软件'

'5','Node4','2','hardware'

1 个答案:

答案 0 :(得分:5)

最后,我能够解决自己的问题。这是修复。更新了ManagedBean

@Component("treeBeanRec")
@Scope("view")
public class TreeBeanRec {

    private TreeNode rootNode;

    private TreeNode selectedNode;

    @Autowired
    TreeService treeService;

    public TreeService getTreeService() {
        return treeService;
    }

    public void setTreeService(TreeService treeService) {
        this.treeService = treeService;
    }

    public TreeBeanRec() {

    }

    @PostConstruct
    public void init() {

        Tree root = treeService.getRoot();

        rootNode = new DefaultTreeNode("Root", null);


        TreeNode t1 = createTree(root, rootNode);


    }

    // Recursive function to create tree

    public TreeNode createTree(Tree treeObj, TreeNode rootNode) {
        TreeNode newNode = new DefaultTreeNode(treeObj, rootNode);

        List<Tree> childNodes1 = treeService.getChildNodes(treeObj.getNodeId());

        for (Tree tt : childNodes1) {
            TreeNode newNode2 = createTree(tt, newNode);
        }

        return newNode;
    }

    public TreeNode getRootNode() {
        return rootNode;
    }

    public void setRootNode(TreeNode node) {
        rootNode = node;
    }

}