jsp动态树的生成.docx

上传人:b****8 文档编号:30479073 上传时间:2023-08-15 格式:DOCX 页数:14 大小:22.19KB
下载 相关 举报
jsp动态树的生成.docx_第1页
第1页 / 共14页
jsp动态树的生成.docx_第2页
第2页 / 共14页
jsp动态树的生成.docx_第3页
第3页 / 共14页
jsp动态树的生成.docx_第4页
第4页 / 共14页
jsp动态树的生成.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

jsp动态树的生成.docx

《jsp动态树的生成.docx》由会员分享,可在线阅读,更多相关《jsp动态树的生成.docx(14页珍藏版)》请在冰豆网上搜索。

jsp动态树的生成.docx

jsp动态树的生成

jsp动态树的生成

已经有两个人问过我这个问题了。

所以我还是把我做的这个东西总结一下。

放上来,一来方便了自己,也方便了他人。

我不反对你把代码那里使用,但是希望你能保留作者。

)谢谢。

 

树的动态生成和一般的静态书是不一样的。

因为动态树中所有的结点或者部分节点不是固定的,可能会随时随着数据库中或者文件中的数据而动态的变动。

对于一般的静态树,我们只需要在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.*;

/**

 *

Title:

教务处工程-专业识别

 *

 *

Description:

树的节点对象

 *

 *

Copyright:

Copyright(c)2004

 *

 *

Company:

中国海洋大学教务处

 *

 *

创建日期:

2004.4.17

 *

 *

修改日期:

2004.4.17

 *@author IPlinger

 *@version1.0

 */

publicclassTreeNote{

 /**

  *节点的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);

 }

}

 

注释我应该已经写的很清楚了,自己看吧。

其中需要一个IDGenerator.getID()的辅助方法ID产生器,这里我给出这个源码,当然你也可以自己写呀

 

IDGenerator.java

packagecn.edu.ouc.jwc.util;

import.*;

/**

 *

名称:

教务处工程-专业识别

 *

 *

描述:

Thisclasshasasinglestaticmetodwhichgeneratesauniqueidbasedonhostnameandtime.

 *

 *

版权:

Copyright(c)2004

 *

 *

单位:

中国海洋大学教务处

 *

创建日期:

2004.4.8

 * 

修改日期:

2004.4.8

 *@authorIPlinger

 *@version1.0

 */

publicclassIDGenerator{

 /**

  *asinglestaticmetodwhichgeneratesauniqueidbasedonhostnameand

  *time.Id=hostname+time.

  *

注意如果id连续产生可能会产生相同的Id,务必谨慎!

  *@returnString一般为主机名加当前时间(毫秒);如果找不到主机明就为UnknownHost加当前时间;

  */

 publicstaticStringgetID(){

   StringId=null;

   try{

     Id=InetAddress.getLocalHost().getHostName();

   }

   catch(UnknownHostExceptionue){

     Id="UnknownHost";

   }

   Id=Id+System.currentTimeMillis();

   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;

 }

}

 

下面是TreeBuilder类

 TreeBuilder.java

packagecn.edu.ouc.jwc.zysb.tree;

importjava.util.*;

importorg.apache.log4j.*;

/**

 *

Title:

教务处工程-专业识别

 *

 *

Description:

树构建器对象.自动生成root节点.包含构建树用的一切方法.

 *

 *

Copyright:

Copyright(c)2004

 *

 *

Company:

中国海洋大学教务处

 *@author IPlinger

 *@version1.0 2004.4.17

 */

publicclassTreeBuilder{

 publicTreeNoteroot=newTreeNote("root");

 //publicstaticLoggerlogger=Logger.getLogger(TreeBuilder.class);//生成记录器;

 publicTreeBuilder(){

   root.setLocation("root");

   root.setParameter("root");

 }

 /**

  *给定一个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(TreeNotetreeNote){

   Stringlocation=treeNote.getLocation();

   if(location!

=null){

     TreeNotelocatedNote=root;//当前定位到的节点;初始时为根节点;

     StringTokenizerloc=newStringTokenizer(location);

     intlocNum=loc.countTokens();

     if(loc.nextToken().equals("root")&&locNum>1){

       //第一层为"root",最后一层为自己,所以i从0开始,循环locNum-2遍;

       for(inti=0;i

         locatedNote=findNoteByParam(locatedNote.getChildren(),

                                       loc.nextToken());

         if(locatedNote==null){

           break;

         }

       }

       if(locatedNote!

=null){

         returnlocatedNote;

       }

     }

   }

   returnnull;

 }

 /**

  *向树中添加一个节点.

  *

  *@paramtreeNote待添加节点对象.

  *@returnture添加成功.

  *  false

  *  添加失败.可能是找不到这个节点的parent节点或着这个节点已经存在!

  */

 publicbooleantreeNoteAdd(TreeNotetreeNote){

   if(treeNote==null){

     //logger.error("thesizeoftreeNoteisnull!

!

!

");

     thrownewNullPointerException();

   }

   TreeNoteparentNote=locateParentNote(treeNote);

   if(parentNote!

=null&&!

isChild(parentNote,treeNote)){

     parentNote.getChildren().add(treeNote);

     returntrue;

   }

   //logger.error("treeNote"+treeNote.getName()+"havenotparentsorisexistent!

can'tbeadded!

!

");

   returnfalse;

 }

 /**

  *把以ArrayList容器装载的一系列节点添加到树.

  *

  *@paramtreeNoteList装入一系列TreeNote的ArrayList.

  */

 publicvoidArrayListAdd(ArrayListtreeNoteList){

   intnoteNum=treeNoteList.size();

   if(noteNum==0){

     //logger.error("thesizeoftreeNoteListiszero!

!

!

");

     thrownewNullPointerException();

   }

   for(inti=0;i

     treeNoteAdd((TreeNote)treeNoteList.get(i));

   }

 }

 /**

  *从所给的存放TreeNote的容器中找到一个其parameter为所给值的节点.

  *

  *@paramlocNoteArrayList.

  *@paramparameterString.

  *@return如果能找到多个则只返回一个TreeNote,其位置在容器中最靠前.找不到则返回null.

  */

 privateTreeNotefindNoteByParam(ArrayListlocNote,Stringparameter){

   if(parameter==null){

     //logger.error("theparameterisnull!

!

!

");

     thrownewNullPointerException();

   }

   TreeNotenoteTemp=null;

   intnum=locNote.size();

   for(inti=0;i

     noteTemp=(TreeNote)locNote.get(i);

     if(noteTemp.getParameter().equals(parameter)){

       returnnoteTemp;

     }

   }

   returnnull;

 }

 //测试用

 publicstaticvoidmain(String[]args){

   TreeBuildermyTree=newTreeBuilder();

   TreeNotetestson=newTreeNote("son");

   testson.setLocation("roottest");

   testson.setParameter("test");

   myTree.root.addChild(testson);

   TreeNotetest=newTreeNote("son");

   test.setLocation("roottest");

   System.out.println(myTree.isChild(myTree.root,test));

 }

}

 

不知道你们主要了吗?

添加节点要根据location来添加,它就相当于这里节点的地址,指明它在整棵树的那个位置。

location其他解释看TreeNote.java中的注释。

ShowTree.java

packagecn.edu.ouc.jwc.zysb.tree;

importjava.util.*;

importorg.apache.log4j.*;

publicclassShowTree{

 //publicstaticLoggerlogger=Logger.getLogger(ShowTree.class);

 privateTreeNotetemp=null;

 privateStringBufferbuf=newStringBuffer();

 /**

  *包装treeHTMLGenerator(ArrayListchiildren)方法

  *

  *@paramchildrenchildren开始传入root节点的孩子

  *@returnString生成的HTML代码

  */

 publicStringHTMLGenerator(TreeNoteroot){

   returntreeHTMLGenerator(root.getChildren());

 }

 /**

  *产生树的HTML代码,为递归程序.

  *

  *@paramchildren开始传入root节点的孩子

  *@return生成的HTML代码

  */

 privateStringtreeHTMLGenerator(ArrayListchildren){

   buf.append("\n");

   intsize=children.size();

   for(inti=0;i

     temp=(TreeNote)children.get(i);

     if(temp.getChildNum()!

=0){//如果这个节点有孩子..

       buf.append("\n");

       if(temp.getURL()!

=null){

         buf.append(

             "

展开阅读全文
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1