Stuts2中tree的实现

发表于:2010-2-05 10:23

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:macoo    来源:51Testing软件测试网采编

#
java

  在struts2中,tree的实现主要是通过struts2-dojo-plugin-XXX.jar中的tree标签来实现的,首先在项目中引入此包,我使用的是sturts2.1.8.1。

  在JSP中使用<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>导入标签标签的同时,别忘了再HEAD标签中添加<sx:head/>,客户端JS文件的导入全指望它了!下面是实现tree的三种方式:

  1.静态方式 也就是tree中的各个节点是在jsp文件中静态写入的,在运行之前就知道其结构,下面是一个Demo:

  JSP

<h2>1.Three Without AJAX</h2>
<sx:treelabel="Tree without AJAX"id="parentId"
    templateCssPath
="/struts/tree.css"showRootGrid="true"
    showGrid
="true">
   
<sx:treenodelabel="child1"id="child1Id">
       
<sx:treenodelabel="grandchild1"id="grandchild1Id"/>
       
<sx:treenodelabel="grandchild2"id="grandchild2Id"/>
       
<sx:treenodelabel="grandchild3"id="grandchild3Id"/>
       
<sx:treenodelabel="grandchild4"id="grandchild4Id"/>
       
<sx:treenodelabel="grandchild5"id="grandchild5Id"/>
   
</sx:treenode>
   
<sx:treenodelabel="child2"id="child2Id"/>
   
<sx:treenodelabel="child3"id="child3Id"/>
   
<sx:treenodelabel="child4"id="child4Id"/>
   
<sx:treenodelabel="child5"id="child5Id"/>
   
<sx:treenodelabel="child6"id="child6Id">
       
<sx:treenodelabel="gChild1"id="gChild1Id"/>
       
<sx:treenodelabel="gChild2"id="gChild2Id"/>
   
</sx:treenode>
   
<sx:treenodelabel="child7"id="child7Id"/>
</sx:tree>

  效果图如上所示。

  2.半动态方式 下面的例子是根据服务器端的项目目录生成的节点树,在jsp中的表现代码上显得非常简捷:

  首先要生成一个用于封装目录下文件结构的类:FileWrapper.Class

  FileWrapper.java

packagetree;

importjava.io.File;
   
publicclassFileWrapper {
   
privateFile file;
   
   
publicFileWrapper(String path){
        file
=newFile(path);
    }
   
publicFileWrapper(File file){
       
this.file=file;
    }
   
publicString getId(){
       
return"file_"+file.hashCode();
    }
   
publicString getName(){
       
returnfile.getName();
    }
   
publicString getAbsolutePath(){
       
returnfile.getAbsolutePath();
    }
   
publicFileWrapper[] getChildren(){
        File[] files
=file.listFiles();
       
if(files!=null&&files.length>0){
           
intlength=files.length;
            FileWrapper[] wrappers
=newFileWrapper[length];
           
for(inti=0;i<length;i++){
                wrappers[i]
=newFileWrapper(files[i]);
            }
           
returnwrappers;
        }
       
else{
           
returnnewFileWrapper[0];
        }
       
    }
}

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号