Extjs Tree + JSON + Struts2Posted on 2008-02-19 09:27 云自无心水自闲 阅读(5073) 评论(13) 编辑 收藏 所属分类: Java 、心得体会 、Struts2 、Ajax 、Extjs最近尝试用extjs来展示树状菜单。着实花了一番功夫。树状菜单的菜单项需要动态加载,而目前版本的extjs中只支持JSON格式的数据。查了一些资料,决定使用struts2的json-plugin。首先按照例子做了一个,但是结果就是不成功,界面上只出来了一个js中生成的root节点,不能加载从后台生成的数据。研究后发现是数据格式有问题。使用json-plugin生成的数据格式如下:{"cls":"folder","id":10,"leaf":false,"children":[{"cls":"file","id":11,"leaf":true,"children":null,"text":"S600"},{"cls":"file","id":12,"leaf":true,"children":null,"text":"SLK200"}],"text":"Benz"}而extjs需要的数据格式如下:[{"cls":"folder","id":10,"leaf":false,"children":[{"cls":"file","id":11,"leaf":true,"children":null,"text":"S600"},{"cls":"file","id":12,"leaf":true,"children":null,"text":"SLK200"}],"text":"Benz"}]区别很小,就只相差最外面的两个方括号。但是少了这两个方括号,在json中,含义迥然不同,前者表示一个对象,而后者表示一个数组。而extjs中 tree的dataloader需要的数据必须是一个数组。而这样的数据格式是json-plugin自动生成的,无法改变。所以,我最后放弃了json -plugin,转而使用json-lib来解决这个问题。1. 下载json-lib, http://json-lib.sourceforge.net/2. lib目录下的jar文件清单:commons-beanutils-1.7.0.jarcommons-collections-3.2.jarcommons-digester-1.6.jarcommons-lang-2.3.jarcommons-logging-1.1.jardom4j-1.6.1.jarezmorph-1.0.4.jarfreemarker-2.3.8.jarjson-lib-2.2.1-jdk15.jarlog4j-1.2.13.jarognl-2.6.11.jarstruts2-core-2.0.11.jarxml-apis-1.0.b2.jarxwork-2.0.4.jar
首先配置web.xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> index.jsp struts2 org.apache.struts2.dispatcher.FilterDispatcher
struts2 /*
然后是struts.xml "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> /menu.jsp
3. 树的节点模型(省略了getter,setter)public class Menu { private int id; private String text; private boolean leaf; private String cls; private List
4. actionpackage com.lab;
import java.util.ArrayList;import java.util.List;
import net.sf.json.JSONArray;
public class MenuAction { private String menuString; private List
menus = new ArrayList
return "success"; }
public String getMenuString() { return menuString; }
public void setMenuString(String menuString) { this.menuString = menuString; }}
5. menu.jsp<%@ taglib prefix="s" uri="/struts-tags" %>
6. html页面和js我使用的就是extjs的example中的reorder.html和reorder.js,更改了reorder.js中treeloader的dataurl: menus.action
This example shows basic drag and drop node moving in a tree. In this implementation there are no restrictions and anything can be dropped anywhere except appending to nodes marked "leaf" (the files).
Drag along the edge of the tree to trigger auto scrolling while performing a drag and drop.
In order to demonstrate drag and drop insertion points, sorting was not enabled.
The data for this tree is asynchronously loaded with a JSON TreeLoader.
The js is not minified so it is readable. See reorder.js.
js:/* * Ext JS Library 2.0.1 * Copyright(c) 2006-2008, Ext JS, LLC. * licensing@extjs.com * * http://extjs.com/license */
Ext.onReady(function(){ // shorthand var Tree = Ext.tree; var tree = new Tree.TreePanel({ el:'tree-div', autoScroll:true, animate:true, enableDD:true, containerScroll: true, loader: new Tree.TreeLoader({ dataUrl:'http://localhost:8080/lab/menus.action' }) });
// set the root node var root = new Tree.AsyncTreeNode({ text: 'Ext JS', draggable:false, id:'source' }); tree.setRootNode(root);
// render the tree tree.render(); root.expand();});
Feedback# re: Extjs Tree + JSON + Struts2 回复 更多评论 2008-02-19 10:41 by 久城哈哈学习了!~返回JSON对象的时候,的确需要注意。# re: Extjs Tree + JSON + Struts2 回复 更多评论 2008-02-19 14:24 by altuurenice tutorial# re: Extjs Tree + JSON + Struts2 回复 更多评论 2008-02-20 16:59 by xt请教一个问题怎样将树下的栏目加上链接?/** Ext JS Library 2.0* Copyright(c) 2006-2007, Ext JS, LLC.* licensing@extjs.com** http://extjs.com/license*/
Ext.onReady(function(){// shorthandvar Tree = Ext.tree;
var tree = new Tree.TreePanel({el:'tree-div',autoScroll:true,animate:true,enableDD:true,containerScroll: true,loader: new Tree.TreeLoader({dataUrl:'http://localhost:8080/extTest/menus.action'})});
// set the root nodevar root = new Tree.AsyncTreeNode({text: 'Ext JS',href:'http://baidu.com',draggable:false,id:'source'});tree.setRootNode(root);
// render the treetree.render();root.expand();});# re: Extjs Tree + JSON + Struts2 [未登录] 回复 更多评论 2008-02-21 08:41 by 云自无心水自闲@xt这需要给树中节点添加点击的侦听函数:Ext.getCmp("menuTree").on('click', function(node) {if ( node.id == 1 ) {Ext.get("center-iframe").dom.src = 'http://www.google.com';} else {node.toggle();}});
其中menuTree是TreePanel的id,center-iframe是嵌入在另一个面板中的iframe的id# re: Extjs Tree + JSON + Struts2 回复 更多评论 2008-02-21 10:57 by xt谢谢!我试试看吧.我的msn:xiaoqiu369@hotmail.com,希望有时间多向你请教!# re: Extjs Tree + JSON + Struts2 回复 更多评论 2008-03-09 00:56 by topsonstar使用json-plugin也可以,只要在TreeLoader的processResponse处理一下,把返回的json对象处理为数组.# re: Extjs Tree + JSON + Struts2 回复 更多评论 2008-03-22 21:07 by hua_y9谢谢,给予了很好的帮助~ :)# re: Extjs Tree + JSON + Struts2 回复 更多评论 2008-03-24 14:10 by laitao有没有struts+ext整合的DEMO,我不知道怎么弄呢,如果有的话,请给我发一份好吗,邮箱1758_love@163.com# re: Extjs Tree + JSON + Struts2 回复 更多评论 2008-04-22 10:32 by ph帮我解决了问题啊# re: Extjs Tree + JSON + Struts2 回复 更多评论 2008-04-25 09:31 by Nicolas内容很棒。多谢# re: Extjs Tree + JSON + Struts2 回复 更多评论 2008-05-23 18:18 by gba我按照文章的步骤来写的,但是虽然能够得到树,而且该树的root节点前面有+号,但是我一点那个+号,却出来不了子结点(并且+号消失),请问lz是怎么回事呢?谢谢了
我得到的json字符串是[{"children":[{"children": [],"cls":"file","id":11,"leaf":true,"text":"S600"},{"children":[],"cls":"file","id":12,"leaf":true,"text":"SLK200"}],"cls":"folder","id":10,"leaf":false,"text":"Benz"},{"children":[{"children":[],"cls":"file","id":21,"leaf":true,"text":"325i"},{"children":[],"cls":"file","id":22,"leaf":true,"text":"X5"}],"cls":"folder","id":20,"leaf":false,"text":"BMW"}]# re: Extjs Tree + JSON + Struts2 [未登录] 回复 更多评论 2008-06-17 13:46 by 坚持到底你好,我按照上面的例子做完之后发现这不是一棵异步加载的树呀!楼主还有没有异步加载的例子了?# re: Extjs Tree + JSON + Struts2 回复 更多评论 2008-06-19 14:06 by usherlight@坚持到底你说的是lazy load还是async load?
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
Duilib中list控件支持ctrl和shif多行选中的实现
[ICML2015]Batch Normalization:Accelerating Deep Network Training by Reducing Internal Covariate Shif
win10系统 微软输入法 于eclipse ctrl+shif+f冲突间接处理办法
Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shif
读LDD3,内存映射与DMA--PAGE_SHIF…
VMware虚拟机安装XP【要先分区,再设置BOOT 启动CD,shif+上移】
更换iBus五笔的左与右Shif
sublime ctrl+shif+f 没用解决办法
idea 对 ctrl + z 的撤销 是 ctrl + shif + z
计算机最早的设计师应用于,计算机应用基础选择题doc.doc
win10自带截图神器:Win+Shift+S
Python基础之文件目录操作
python简述目录_Python基础之文件目录操作(示例代码)
tp5 如何做数据采集
任务2-7(服务器字体+阿里巴巴矢量库)
html标签(1):h1~h6,p,br,pre,hr
TI 电量计介绍与芯片选型指南
几款TI电源芯片简介
TI DSP芯片C2000系列读取FLASH数据
德州仪器(Ti)平台嵌入式开发基础
TI三相电机智能栅极驱动芯片特点分类
省选模拟(12.08) T3 圈圈圈圈圈圈圈圈
Hadoop生态圈技术栈(上)
大数据开发基础入门与项目实战(三)Hadoop核心及生态圈技术栈之6.Impala交互式查询
小猿圈之Linux下Mysql 操作命令
大数据Hadoop生态圈常用面试题
大数据开发基础入门与项目实战(三)Hadoop核心及生态圈技术栈之4.Hive DDL、DQL和数据操作
备战Noip2018模拟赛11(B组)T3 Monogatari 物语
【智能优化算法-圆圈搜索算法】基于圆圈搜索算法Circle Search Algorithm求解单目标优化问题附matlab代码
NYOJ 78 圈水池
递归问题 跑道 汽车 绕圈问题 Python实现
Hadoop生态圈(三):MapReduce
微信公众账号
微信扫一扫加关注