Webwork/Struts2 Tree标签(树形控件)的使用

最近开发中需要使用ww的 tree 标签,几乎花了一天的时间才搞定,非常郁闷。现解决了,特总结一下。首先,要知道webwrok是struts2的前身,标签方面也是差不多的。所以一起讲,我习惯把webwork简称为ww。

1.web.xml中不要使用ServletDispacher作为前端控制器,而应该使用FilterDispatcher,前者被官方废弃了,因为它至少不支持ajax标签。而后者会自动把针对css、js的以 /webwork 开头的请求映射到类包内的css、js等静态文件。所以,后者才支持ajax标签。

2.ww的ajax控件和校验大多是基于dojo脚本库的。所以,com.opensymphony.webwork.static包内存放了dojo的js文件,这是需要在页面内引用的。因为tree是由dojo来生成的。

3.ww中,要想使用tree等复杂标签(或称为控件),需要在jsp内加一个<ww:head theme="ajax" debug="true" /> 标签,它的作用是引用dojo的js文件,也就是说你不用写代码去引用了,该标签会生成。debug属性可以去掉,不过开发时方便一点。

4.同样,要使用tree标签,也需要多设置一个theme="ajax",比如我的:
    <ww:tree rootNode="resources" nodeIdProperty="id" nodeTitleProperty="name"
    childCollectionProperty="childs" id="resources" name="resources" theme="ajax">
    </ww:tree>

5.这是很重要的一点。作为tree的来源的对象,应该编写一个专门的类,它应该有一个返回子节点集合的方法,我这里是childCollectionProperty="childs";还应该有对应节点id和节点文字的字段,我这里分别是id和name。因为我Action中命名为resources,所以根节点是rootNode="resources"。

6.还需要注意的是,Node对象的childs最好在声明时就实例化(代码见下面),否则如果叶节点没有子节点时,getChilds()方法返回null那么freemarker会报模板解析错误(这个问题我花了很长时间才解决,还是自己去调试freemarker的源码的,因为ww和struts2官方都没有说明这一点)。好了,下面是我的Action、Node对象、JSP的代码:


Node对象:
public class ResourceNode{

    private List<ResourceNode> childs = new ArrayList<ResourceNode>();//子节点集合
    private Integer id;
    private String name;//资源名称
    private String url;
    private Integer parent_id;//父类id
    private Integer is_dir;

    //getter和setter省略.......
}



Action:
public class SecurityResourceAction extends WWAction{

    private SecurityResourceManager securityResourceManager;
    private ResourceNode resources;//根节点
   
    /**
    * 到列表页面,显示一个树形
    * @return
    */
    public String addRole(){
        resources = securityResourceManager.queryAllResourceAsTree(null);
        return "role_add";
    }

    public ResourceNode getResources() {
        return resources;
    }

    public void setResources(ResourceNode resources) {
        this.resources = resources;
    }
}


JSP:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../commons/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理中心 - 角色列表 </title>
<meta name="robots" content="noindex, nofollow" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<ww:head theme="ajax" debug="true" />

</head>
<body>

<form action="../adminSec!addRole.do">
    角色名称:<input type="text" name="role.name" />
    角色说明:<input type="text" name="role.remark" />
    <ww:tree rootNode="resources" nodeIdProperty="id" nodeTitleProperty="name"
    childCollectionProperty="childs" id="resources" name="resources" theme="ajax">
    </ww:tree>

</form>

<jsp:include page="../../admin/footer.jsp"/>
</body>
</html>


参考/鸣谢资料:
http://blog.csdn.net/yehell/archive/2008/01/08/2030357.aspx
http://www.opensymphony.com/webwork/wikidocs/ajax%20head%20template.html
http://www.opensymphony.com/webwork/wikidocs/ajax%20theme.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值