rss· 导航· 设为首页· 加入收藏
当前位置: 火魔网 » 程序开发 » Javascript

Struts2+JSON开发Extjs TreePanel

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

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

Struts2+JSON开发Extjs TreePanel

剩下就没什么大问题了,配置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();

});

顶一下
(1)
踩一下
(0)