动态树.docx
《动态树.docx》由会员分享,可在线阅读,更多相关《动态树.docx(12页珍藏版)》请在冰豆网上搜索。
动态树
帖子地址:
收藏到我的口袋 复制给好友举报
你查询的问题是:
动态树
树的动态生成和一般的静态书是不一样的。
因为动态树中所有的结点或者部分节点不是固定的,可能会随时随着数据库中或者文件中的数据而动态的变动。
对于一般的静态树,我们只需要在jsp页面中包含js代码就可以了。
那么动态树就需要javabean的支持了。
主要文件
TreeNote.java:
树的结点类。
TreeBuilder.java:
树的构造器。
ShowTree.java:
为了减少页面中的java语句,我们在这里构造生成树需要的html代码。
BuildyourTree.java:
在这里构造你的树。
你也可以写自己的方法,这里只是个例子。
下面一个一个文件的来看:
TreeNote.java
packagecn.edu.ouc.jwc.zysb.tree;
importjava.util.*;
importcn.edu.ouc.jwc.util.*;
/**
*
/**
*节点的Id,生成对象时此Id会被自动生成.
*/
privateStringId=null;
/**
*描述节点在树中的位置.必须准许一定的格式.其格式规则如下:
*1.根节点值为"root";
*2.其他节点的值为"rootsecondLevelparameterthirdLevelParameter...thisLevelParameter".
*举例我要添加专业节点其location内容如下:
"rootyhzhuanye",其中yh为这个专业所属院的parameter,zhuanye为本专业的parameter.
*/
privateStringlocation=null;
/**
*节点名字.要显示在页面上的东东.就是每个节点上显示的名字.
*/
privateStringname=null;
/**
*每个节点对应的URL.
*/
privateStringURL=null;
/**
*也就是本层的参数,代表本层,将要添加到location的末尾中.
*/
privateStringparameter=null;
/**
*容器包装其所有的孩子节点.
*/
privateArrayListchildren=newArrayList();
publicTreeNote(){
Id=IDGenerator.getID();
}
publicTreeNote(intseed){
Id=IDGenerator.getID(seed);
}
publicTreeNote(Stringname){
Id=IDGenerator.getID();
this.name=name;
}
publicTreeNote(Stringname,intseed){
Id=IDGenerator.getID(seed);
this.name=name;
}
publicStringgetId(){
returnId;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetURL(){
returnURL;
}
publicvoidsetURL(StringURL){
this.URL=URL;
}
publicvoidsetLocation(Stringlocation){
this.location=location;
}
publicStringgetLocation(){
returnlocation;
}
publicArrayListgetChildren(){
returnchildren;
}
publicvoidsetChildren(ArrayListchildren){
this.children=children;
}
publicStringgetParameter(){
returnparameter;
}
publicvoidsetParameter(Stringparameter){
this.parameter=parameter;
}
/**
*返回孩子的数目.
*
*@return孩子的数目,无孩子为"0".
*/
publicintgetChildNum(){
returnchildren.size();
}
/**
*增加一个孩子.
*
*@paramchildTreeNote.
*/
publicvoidaddChild(TreeNotechild){
children.add(child);
}
/**
*根据索引得到一个孩子.
*
*@paramindex从"0"开始的索引."0"代表第一个孩子.
*@returnTreeNote
*/
publicTreeNotegetChild(intindex){
return(TreeNote)children.get(index);
}
}
Title:
教务处工程-专业识别
*
*
Description:
树的节点对象
*
*
Copyright:
Copyright(c)2004
*
*
Company:
中国海洋大学教务处
*
*
创建日期:
2004.4.17
*
*
修改日期:
2004.4.17
*@authorIPlinger
*@version1.0
*/
publicclassTreeNote{
注释我应该已经写的很清楚了,自己看吧。
其中需要一个IDGenerator.getID()的辅助方法ID产生器,这里我给出这个源码,当然你也可以自己写呀
IDGenerator.java
packagecn.edu.ouc.jwc.util;
import.*;
/**
*
/**
*asinglestaticmetodwhichgeneratesauniqueidbasedonhostnameand
*time.Id=hostname+time.
*
returnId;
}
/**
*asinglestaticmetodwhichgeneratesauniqueidbasedonhostnameand
*timeandseed.Id=hostname+time+seed.
*
此方法的引入无非是为了避免上一方法的缺点,但注意传入的seed在生成一系列id时务必是不同的;比如可以传入循环中的"i".
*@paramseed
*@returnString一般为主机名+当前时间(毫秒)+seed;如果找不到主机明就为UnknownHost加当前时间+seed;
*/
publicstaticStringgetID(intseed){
StringId=null;
try{
Id=InetAddress.getLocalHost().getHostName();
}
catch(UnknownHostExceptionue){
Id="UnknownHost";
}
Id=Id+System.currentTimeMillis()+String.valueOf(seed);
returnId;
}
}
注意如果id连续产生可能会产生相同的Id,务必谨慎!
*@returnString一般为主机名加当前时间(毫秒);如果找不到主机明就为UnknownHost加当前时间;
*/
publicstaticStringgetID(){
StringId=null;
try{
Id=InetAddress.getLocalHost().getHostName();
}
catch(UnknownHostExceptionue){
Id="UnknownHost";
}
Id=Id+System.currentTimeMillis();
名称:
教务处工程-专业识别
*
*
描述:
Thisclasshasasinglestaticmetodwhichgeneratesauniqueidbasedonhostnameandtime.
*
*
版权:
Copyright(c)2004
*
*
单位:
中国海洋大学教务处
*
创建日期:
2004.4.8
*
修改日期:
2004.4.8
*@authorIPlinger
*@version1.0
*/
publicclassIDGenerator{
下面是TreeBuilder类
TreeBuilder.java
packagecn.edu.ouc.jwc.zysb.tree;
importjava.util.*;
importorg.apache.log4j.*;
/**
*
/**
*给定一个parent节点和一个child节点,判断这个child节点是否属于这个parent.
*
*@paramparentparent节点.
*@paramchildchild节点.
*@returntrue所给的两个节点父子关系正确.
*false所给的两个节点不存在指定的父子关系.
*/
privatebooleanisChild(TreeNoteparent,TreeNotechild){
if(parent==null){
//logger.error("treeNoteparentnull!
!
!
");
thrownewNullPointerException();
}
if(child!
=null&&
findNoteByParam(parent.getChildren(),child.getParameter())!
=null){
returntrue;
}
returnfalse;
}
/**
*在树中找出所给节点的双亲节点.
*
*@paramtreeNote
*@returnTreeNote如果没有找到返回null.
*/
privateTreeNotelocateParentNote(TreeNotetreeN