1

我正在使用 primefaces 树组件。我在使用递归从数据库构建树时遇到问题。我从数据库中查看了这个问题Primefaces tree。尽管如此,我仍然无法构建树。我在 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;
    }


}

托管豆

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() 获取列表;

树.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 节点类型

“1”、“项目名称”、“0”、“项目名称”

'2'、'Node1'、'1'、'硬件'

'3'、'Node2'、'1'、'软件'

'4'、'Node3'、'1'、'软件'

'5'、'Node4'、'2'、'硬件'

4

1 回答 1

7

最后,我能够解决我自己的问题。这是修复。更新了 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;
    }

}
于 2012-09-13T11:32:30.663 回答