jsf之三方组件包myfaces之tree2动态应用

首先,来建个树型表数据,网上google一击,差不多类似
/**
--树形菜单
  节点编号
  节点名称
  节点链接址
  些节点父节点
  是否最底节点
  节点排序(一般用在同级节点上)
  节点说明 
*/



drop table EPTreeTable

create table EPTreeTable
(
  TreeNodeId int identity(1,1) constraint pk_TreeNode primary key,
  TreeNodeName varchar(50) not null,
  TreeNodeLink varchar(100) ,
  TreeNodeFatherId int default 0, --0 为最高层
  TreeNodeIsBottom bit default 1, --1 为底层 0为非底层
  TreeNodeCompositor int default 1, -- 默认排在最后面
  TreeNodeExplain varchar(200)
)

二、建立一个TO对象

package ep.pojo;
import java.io.Serializable;
/**
* @author fangbiao
* 树TO对象
*/
public class TreePojo implements Serializable{
/**
* 节点编号
*/
private int treeNodeId;
/**
* 节点名称
*/
private String treeNodeName;
/**
* 节点操作目的地
*/
private String treeNodeLink;
/**
* 父节点
*/
private int treeParentNodeId;
/**
* 是否底层节点
*/
private boolean treeNodeIsBottom;
/**
* 结点排序(同级)
*/
private int treeNodeCompositor;
/**
* 节点备注
*/
private String treeNodeExplain;
/**
* 节点权限
*/
private String treeNodeControl;
/**
* @return treeNodeControl
*/
public String getTreeNodeControl() {
return treeNodeControl;
}
/**
* @param treeNodeControl 要设置的 treeNodeControl
*/
public void setTreeNodeControl(String treeNodeControl) {
this.treeNodeControl = treeNodeControl;
}
/**
* @return treeNodeCompositor
*/
public int getTreeNodeCompositor() {
return treeNodeCompositor;
}
/**
* @param treeNodeCompositor 要设置的 treeNodeCompositor
*/
public void setTreeNodeCompositor(int treeNodeCompositor) {
this.treeNodeCompositor = treeNodeCompositor;
}
/**
* @return treeNodeExplain
*/
public String getTreeNodeExplain() {
return treeNodeExplain;
}
/**
* @param treeNodeExplain 要设置的 treeNodeExplain
*/
public void setTreeNodeExplain(String treeNodeExplain) {
this.treeNodeExplain = treeNodeExplain;
}
/**
* @return treeNodeId
*/
public int getTreeNodeId() {
return treeNodeId;
}
/**
* @param treeNodeId 要设置的 treeNodeId
*/
public void setTreeNodeId(int treeNodeId) {
this.treeNodeId = treeNodeId;
}
/**
* @return treeNodeIsBottom
*/
public boolean isTreeNodeIsBottom() {
return treeNodeIsBottom;
}
/**
* @param treeNodeIsBottom 要设置的 treeNodeIsBottom
*/
public void setTreeNodeIsBottom(boolean treeNodeIsBottom) {
this.treeNodeIsBottom = treeNodeIsBottom;
}
/**
* @return treeNodeLink
*/
public String getTreeNodeLink() {
return treeNodeLink;
}
/**
* @param treeNodeLink 要设置的 treeNodeLink
*/
public void setTreeNodeLink(String treeNodeLink) {
this.treeNodeLink = treeNodeLink;
}
/**
* @return treeNodeName
*/
public String getTreeNodeName() {
return treeNodeName;
}
/**
* @param treeNodeName 要设置的 treeNodeName
*/
public void setTreeNodeName(String treeNodeName) {
this.treeNodeName = treeNodeName;
}
/**
* @return treeParentNodeId
*/
public int getTreeParentNodeId() {
return treeParentNodeId;
}
/**
* @param treeParentNodeId 要设置的 treeParentNodeId
*/
public void setTreeParentNodeId(int treeParentNodeId) {
this.treeParentNodeId = treeParentNodeId;
}
}
三、建立一棵递归的树,网上资料也大部分是类似的,有的是js排序的,有的是数据库排序,我用的也是一种通用方法(此方法也是从网上参考而来,感谢那位兄弟)
我将提供两种方式,一是只读一次数据库,程序排序
//这就是only读一次数据
           private List testList = null;
private int flag = 0;
public List getTestList(){
if(testList == null){
if(flag == 0){
flag = 1;
testList =  itbs.getManagerTree();
return testList;
}
return testList;
}
return testList;
}
//对树列表数据(进行子树生成)
public List getChildrenList(TreePojo tp){
List list = getTestList();
int listLength = list.size();
List newReturnList = new ArrayList();
if(listLength == 0)
return newReturnList;

List leaveList = new ArrayList();
for(int i=0;i<listLength;i++){

TreePojo treePojo =  (TreePojo)list.get(i);
int treePojoTreeParantNodeId = treePojo.getTreeParentNodeId();
int tpTreeNodeId = tp.getTreeNodeId();
if(treePojoTreeParantNodeId == tpTreeNodeId){
newReturnList.add(treePojo);
}else{
leaveList.add(treePojo);
}
}
list = leaveList;
return newReturnList;
}
//成生一棵树
private synchronized TreeNode doTurnToTree(TreePojo  tp){
List list = getChildrenList(tp);
int treeLength = list.size();
TreeNodeBase personNode = null;

if(tp.getTreeParentNodeId() == 0){
//开始树
personNode = new TreeNodeBase("person",tp.getTreeNodeName(),false);

}else{
if(treeLength >0){
// 有子树
personNode = new TreeNodeBase("folder",tp.getTreeNodeName(),false);
}else{
// 叶子
personNode = new TreeNodeBase("document",tp.getTreeNodeName(),tp.getTreeNodeLink(),true);
}

}
Iterator iterator = list.iterator();
while(iterator.hasNext()){
TreePojo treePojo = (TreePojo)iterator.next();
personNode.getChildren().add(this.doTurnToTree(treePojo));
}
return personNode;
}

第一种方式OK!!

第二种方式就是把
List list = getChildrenList(tp);
改为
List list = itbs.getTree(tp);
这句是需要读取数据库的

刚好有事,明天续........

你可能感兴趣的:(JSF,Google)