1
我正在使用primefaces樹組件。我遇到了使用遞歸從數據庫構建樹的問題。我看過這個問題Primefaces tree from database。但是,我無法構建樹。 我在for循環中有一個getChildNodes列表,因爲它是遞歸函數,所以它被重複調用。我無法修復它。以下是我的代碼。使用遞歸從數據庫構建primefaces樹
實體
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是除了根節點的所有節點的列表。我從春季服務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
節點ID數據PARENTID節點類型
'1', '項目名稱', '0', '項目名'
'2', '節點1',「1 」, '硬件'
'3', '節點2', '1', '軟件'
'4', '節點3', '1', '軟件'
'5','Node4','2','hardware'