Struts2+JSON开发ExtJS

我一开始想用struts2的json-plugin,不过看网上有人说json-plugin生成的json数据格式上有点问题,所以推荐使用 json-lib 插件,我也没多少时间去验证json-plugin到底可不可以,所以就按他们的说的方法采用json-lib来实现。

采用json-lib遇到的最大问题,就是json-lib的依赖包以及版本的一致性问题。jar包大家可以从网上搜一下,我调试通过的版本如下:


剩下就没什么大问题了,配置struts.xml、Action如何输出,以及jsp页面的写法,我把这三部分的代码贴出来,按这个配置一下就可以了。

struts.xml:

<action name="getMenu" class="getUserAction">

<result>/main.jsp</result>

</action>

GetMenuAction:

public class GetMenuAction extends ActionSupport {

 

@SuppressWarnings("unchecked")

@Override

public String execute() throws Exception {

System.out.println("getMenuAction is running.");

HttpServletResponse response = ServletActionContext.getResponse();

response.setContentType("text/javascript;charset=GBK");

PrintWriter out = response.getWriter();

 

Map map = new HashMap();

map.put("id", 1);

map.put("href", "javascript:showUser();");

map.put("text", "查看用户");

map.put("leaf", true);

JSONObject jsonObject = JSONObject.fromObject(map);

System.out.println(jsonObject);

out.write("[" + jsonObject.toString() + "]");

 

return null;

}

}

main.jsp:

Ext.onReady(function(){

    // shorthand

    var Tree = Ext.tree;

 

    var tree = new Tree.TreePanel({

        useArrows: true,

        autoScroll: true,

        animate: true,

        enableDD: true,

        containerScroll: true,

        border: false,

        // auto create TreeLoader

        dataUrl: 'getMenu.eri',

 

        root: {

            nodeType: 'async',

            text: '用户管理',

            draggable: false,

            id: 'src'

        }

    });

 

    // render the tree

    tree.render('west');

    tree.getRootNode().expand();

});

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值