Struts 2与AJAX(第一部分)(六)

42 篇文章 0 订阅
3、<s:tree />

树是是比较常用的数据结构,因为它可以很好地体现真实世界中对象之间的关系。<s:tree />的使用也相对简单,但需要说明的是——Struts 2.0.6 GA版本的<s:tree />是有BUG的,大家可以点击这个链接https://issues.apache.org/struts/browse/WW-1813了解详细的情况。这个BUG主要是在<s:tree />的通过“treeCollapsedTopic”、“treeExpandedTopic”和“treeSelectedTopic”设定的话题(Topic)都没有起作用,上述链接相应给出了解决方法,但我认为该方法太麻烦(需要自己重新编译和打包Struts 2),所以下面的例子,我将另辟徯径,请参考以下代码。

<%@ page language="java" contentType="text/html; charset=utf-8"
     pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!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>Struts 2 AJAX - Tree</title>
        <s:head theme="ajax" debug="true" />
        <script type="text/javascript">
            function treeNodeSelected(arg) {
                 alert(arg.source.title + ' selected');
             }
             dojo.addOnLoad(function() {                
                var s = dojo.widget.byId('parentId').selector;                
                 dojo.event.connect(s, 'select', 'treeNodeSelected');
             });
        </script>
    </head>
    <body>
        <h2>
             Tree
        </h2>
        <div style="float:left; margin-right: 50px;">
            <s:tree label="parent" id="parentId" theme="ajax"
                 templateCssPath="/struts/tree.css" showRootGrid="true"
                 showGrid="true">
                <s:treenode theme="ajax" label="child1" id="child1Id">
                    <s:treenode theme="ajax" label="grandchild1" id="grandchild1Id" />
                    <s:treenode theme="ajax" label="grandchild2" id="grandchild2Id" />
                    <s:treenode theme="ajax" label="grandchild3" id="grandchild3Id" />
                </s:treenode>
                <s:treenode theme="ajax" label="child2" id="child2Id" />
                <s:treenode theme="ajax" label="child3" id="child3Id" />
                <s:treenode theme="ajax" label="child4" id="child4Id" />
                <s:treenode theme="ajax" label="child5" id="child5Id">
                    <s:treenode theme="ajax" label="gChild1" id="gChild1Id" />
                    <s:treenode theme="ajax" label="gChild2" id="gChild2Id" />
                </s:treenode>
            </s:tree>
        </div>
    </body>
</html>清单10 WebContent/Tree.jsp

因为Dojo的树控件,即使在没有设定“selector”情况下,也会自动生成一个默认的Selector,所以只要将其事件绑定到特定的事件处理函数即可。

打开http://localhost:8080/Struts2_Ajax/Tree.jsp,点击任一树节点,页面如下图所示:

图3 Tree.jsp页面输出 
图3 Tree.jsp页面输出

总结

我原本打算用一篇文章写完这个“Struts 2与AJAX”。不过在写的过程中,发现内容越来越多。如果勉强写成一篇,朋友们读起来也会很麻烦,所以我决定分开几部分,本文为第一部分。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值