stuts2中tree的实现

在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:

  

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

2010020413232511.jpg

效果图如上所示。

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

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

 

ContractedBlock.gif ExpandedBlockStart.gif FileWrapper.java
 
      
package tree;

import java.io.File;

public class FileWrapper {
private File file;

public FileWrapper(String path){
file
= new File(path);
}
public FileWrapper(File file){
this .file = file;
}
public String getId(){
return " file_ " + file.hashCode();
}
public String getName(){
return file.getName();
}
public String getAbsolutePath(){
return file.getAbsolutePath();
}
public FileWrapper[] getChildren(){
File[] files
= file.listFiles();
if (files != null && files.length > 0 ){
int length = files.length;
FileWrapper[] wrappers
= new FileWrapper[length];
for ( int i = 0 ;i < length;i ++ ){
wrappers[i]
= new FileWrapper(files[i]);
}
return wrappers;
}
else {
return new FileWrapper[ 0 ];
}

}
}

新建一个Action,用于向ValueStack中添加生成目录树所需的一些数据,

 

ContractedBlock.gif ExpandedBlockStart.gif DynamicTreeAction.java
 
      
package tree;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;
public class DynamicTreeAction extends ActionSupport implements ServletRequestAware {

private static final long serialVersionUID = - 8571025932301813496L ;

private FileWrapper root;
private HttpServletRequest request;

public FileWrapper getRoot(){
return root;
}
public void setServletRequest(HttpServletRequest request){
this .request = request;
}
public String execute(){
root
= new FileWrapper(request.getSession().getServletContext().getRealPath( " / " ));
return SUCCESS;
}
}

在xml中配置action

 

ContractedBlock.gif ExpandedBlockStart.gif struts.xml
 
      
< package name ="ajax" namespace ="/ajax" extends ="json-default" >
< action name ="DynamicTree" class ="tree.DynamicTreeAction" >
< result > /tree.jsp </ result >
</ action >
</ package >

JSP文件内容:

 

 
     
< h2 > 2.Tree with AJAX </ h2 >
< sx:tree id ="appFiles" rootNode ="root"
nodeTitleProperty
="name" nodeIdProperty ="id"
childCollectionProperty
="children" />

看起来非常简捷,其实struts已经帮我们干了好多事情,

 

OK,查看一下效果:

2010020413333353.jpg

posted on 2010-02-04 13:36 macoo 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/macooma/archive/2010/02/04/1663569.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值