首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

[转]ExtJs + Struts2 + JSON 程序小结

2012-10-08 
[转]ExtJs + Struts2 + JSON 程序总结最近一直都在看EXTJS的东西,然后自己实践了下,界面倒是蛮漂亮的,但是

[转]ExtJs + Struts2 + JSON 程序总结

最近一直都在看EXTJS的东西,然后自己实践了下,界面倒是蛮漂亮的,但是一旦涉及到与服务器端进行数据互动麻烦就出来了,本来下了个例子确发现是用DWR的,觉得我既然用了STRUTS2作为MVC的框架,我觉得这个框架还是很不错的,觉得还是把EXTJS整合到一起更好些,找了相关的资料,跟着前辈做了下例子,发现完全不是那么回事,只好自己慢慢摸索,终于把数据交互的问题解决了,所以记录之以便查阅! 还是从底层开始说吧,拿最经典的例子来解说吧,订单和客户的关系显然是n:1的关系,我hibernate不是用的声明方式所以就用的xml方式做的那么相应的hbm.xml文件如下:?

?相应的MODEL的JAVA我就不写了,只是做个例子而已,呵呵!相应的DAO SERVICE 我都不写了,这个不是我讨论的范围,那么我想在页面上显示所有的信息,那么在OrderAction中我定义了一个getAllOrder的方法,然后通过struts2配置action让EXTJS与服务器数据进行数据交互。因为EXTJS是支持JSON数据格式的,所以我用了JSON-LIB(json-lib-2.2.1-jdk15.jar)这个东东,它还依赖另外的3个包:commons-beanutils-1.7.1-20061106.jar,commons-collections-3.2.1.jar,ezmorph-1.0.4.jar。好了万事俱备只欠东风了,我的getAllOrder方法如下:

[转]ExtJs + Struts2 + JSON 程序小结import?java.text.DateFormat;
[转]ExtJs + Struts2 + JSON 程序小结import?java.text.ParseException;
[转]ExtJs + Struts2 + JSON 程序小结import?java.text.SimpleDateFormat;
[转]ExtJs + Struts2 + JSON 程序小结import?java.util.Date;
[转]ExtJs + Struts2 + JSON 程序小结import?java.util.List;
[转]ExtJs + Struts2 + JSON 程序小结import?net.sf.json.*;
[转]ExtJs + Struts2 + JSON 程序小结//具体的那些serivce的包引入我就省略了
[转]ExtJs + Struts2 + JSON 程序小结public?class?OrderAction?extends?ActionSupport
[转]ExtJs + Struts2 + JSON 程序小结{
[转]ExtJs + Struts2 + JSON 程序小结???private?static?final?long?serialVersionUID?=?-5092865658281004791L;
[转]ExtJs + Struts2 + JSON 程序小结????private?IOrderSerivce?orderSerivce;
[转]ExtJs + Struts2 + JSON 程序小结????private?String?jsonString;//这个就是中转站了
[转]ExtJs + Struts2 + JSON 程序小结????private?List<Order>?orderList;//这个是数据链表
[转]ExtJs + Struts2 + JSON 程序小结????private?int?totalCount;//这个是extjs用来分页
[转]ExtJs + Struts2 + JSON 程序小结?????public?String?getJsonString()
[转]ExtJs + Struts2 + JSON 程序小结????{
[转]ExtJs + Struts2 + JSON 程序小结????????return?jsonString;
[转]ExtJs + Struts2 + JSON 程序小结????}
[转]ExtJs + Struts2 + JSON 程序小结?????public?void?setJsonString(String?jsonString)
[转]ExtJs + Struts2 + JSON 程序小结????{
[转]ExtJs + Struts2 + JSON 程序小结????????this.jsonString?=?jsonString;
[转]ExtJs + Struts2 + JSON 程序小结????}
[转]ExtJs + Struts2 + JSON 程序小结????public?int?getTotalCount()
[转]ExtJs + Struts2 + JSON 程序小结????{
[转]ExtJs + Struts2 + JSON 程序小结????????return?totalCount;
[转]ExtJs + Struts2 + JSON 程序小结????}
[转]ExtJs + Struts2 + JSON 程序小结????public?void?setTotalCount(int?totalCount)
[转]ExtJs + Struts2 + JSON 程序小结????{
[转]ExtJs + Struts2 + JSON 程序小结????????this.totalCount?=?totalCount;
[转]ExtJs + Struts2 + JSON 程序小结????}
[转]ExtJs + Struts2 + JSON 程序小结????public?List<Air>?getOrderList()
[转]ExtJs + Struts2 + JSON 程序小结????{
[转]ExtJs + Struts2 + JSON 程序小结????????return?orderList;
[转]ExtJs + Struts2 + JSON 程序小结????}
[转]ExtJs + Struts2 + JSON 程序小结????public?void?setOrderList(List<Order>?orderList)
[转]ExtJs + Struts2 + JSON 程序小结????{
[转]ExtJs + Struts2 + JSON 程序小结????????this.orderList?=?orderList;
[转]ExtJs + Struts2 + JSON 程序小结????}
[转]ExtJs + Struts2 + JSON 程序小结????public?void?setOrderSerivce(OrderSerivce?orderSerivce)
[转]ExtJs + Struts2 + JSON 程序小结????{
[转]ExtJs + Struts2 + JSON 程序小结????????this.orderSerivce?=?orderSerivce;
[转]ExtJs + Struts2 + JSON 程序小结????}
[转]ExtJs + Struts2 + JSON 程序小结????public?String?getAllAir()
[转]ExtJs + Struts2 + JSON 程序小结????{
[转]ExtJs + Struts2 + JSON 程序小结????????orderList?=?orderSerivce.getOrderAll();
[转]ExtJs + Struts2 + JSON 程序小结????????this.setTotalCount(orderList.size());
[转]ExtJs + Struts2 + JSON 程序小结????????
[转]ExtJs + Struts2 + JSON 程序小结????????JSONArray?array?=?JSONArray.fromObject(orderList);
[转]ExtJs + Struts2 + JSON 程序小结//哈哈,就是在这里进行转换的
[转]ExtJs + Struts2 + JSON 程序小结????????this.jsonString?=?"{totalCount:"+this.getTotalCount()+",results:"+array.toString()+"}";
[转]ExtJs + Struts2 + JSON 程序小结????return?SUCCESS;
[转]ExtJs + Struts2 + JSON 程序小结????}
[转]ExtJs + Struts2 + JSON 程序小结}

接下来再是什么,哦,是的,应该是STRUTS的配置了,哈哈

<!DOCTYPE?struts?PUBLIC
[转]ExtJs + Struts2 + JSON 程序小结????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"
[转]ExtJs + Struts2 + JSON 程序小结????"http://struts.apache.org/dtds/struts-2.0.dtd">
[转]ExtJs + Struts2 + JSON 程序小结
[转]ExtJs + Struts2 + JSON 程序小结<struts>
[转]ExtJs + Struts2 + JSON 程序小结??????<package?name="order"?extends="struts-default">
[转]ExtJs + Struts2 + JSON 程序小结<action?name="getAllOrder"?class="orderAction"?method="getAllOrder">
[转]ExtJs + Struts2 + JSON 程序小结????????????<result?name="success"?>jsondata.jsp</result>


[转]ExtJs + Struts2 + JSON 程序小结????????</action>
[转]ExtJs + Struts2 + JSON 程序小结??????</package>
[转]ExtJs + Struts2 + JSON 程序小结</struts>

好的,看到jsondata.jsp了么,这里就是要放数据的地方,看看是什么吧!

<%@?page?language="java"?import="java.util.*"?pageEncoding="utf-8"%>
[转]ExtJs + Struts2 + JSON 程序小结<%@?taglib?prefix="s"?uri="/struts-tags"?%>
[转]ExtJs + Struts2 + JSON 程序小结<s:property?value="jsonString"?escape="false"?/>

?是的,就是这么简单的一个代码!终于要到前台了,该露脸了,呵呵,前台代码最关键的也就是JS代码,那么我也就只贴JS了相信大家看过后都会自己弄清楚的!

Ext.onReady(function(){
[转]ExtJs + Struts2 + JSON 程序小结????Ext.BLANK_IMAGE_URL?=?'ext/resources/images/default/s.gif';?
[转]ExtJs + Struts2 + JSON 程序小结????Ext.QuickTips.init();
[转]ExtJs + Struts2 + JSON 程序小结????var?xg?=?Ext.grid;
[转]ExtJs + Struts2 + JSON 程序小结????//这里就是设置解析格式的地方,一定要和你的Model一样,要不然可是什么都得不到哦~~~~
[转]ExtJs + Struts2 + JSON 程序小结????var?rd?=?new?Ext.data.JsonReader({
[转]ExtJs + Struts2 + JSON 程序小结????????????????//总记录数
[转]ExtJs + Struts2 + JSON 程序小结????????????????totalProperty:?'totalCount',?
[转]ExtJs + Struts2 + JSON 程序小结????????????????//哪儿是数据的头,可以看action里面是怎么定义数据格式的,这里就是如何解析的???????????
?????????????????? ?root:?'results',?
[转]ExtJs + Struts2 + JSON 程序小结????????????????//有那些字段呢?
[转]ExtJs + Struts2 + JSON 程序小结????????????????fields:[
[转]ExtJs + Struts2 + JSON 程序小结?????????????????????????{name:'orderId'},
??????????????????????????????????????{name:'desn'},
[转]ExtJs + Struts2 + JSON 程序小结?????????????????????????{name:'booktime'},
[转]ExtJs + Struts2 + JSON 程序小结?????????????????????????{name:'company'},
[转]ExtJs + Struts2 + JSON 程序小结?????????????????????????{name:'name'},
[转]ExtJs + Struts2 + JSON 程序小结????????????????????????????//这里就是对custom对象进行映射的地方????????????????????????
??????????????????????????????????????{name:'customId'?,mapping:'custom.customId'},
[转]ExtJs + Struts2 + JSON 程序小结?????????????????????????{name:'customName',mapping:'custom.customName'}
[转]ExtJs + Struts2 + JSON 程序小结?????????????????????????]
[转]ExtJs + Struts2 + JSON 程序小结?????????????????????????????????????});
[转]ExtJs + Struts2 + JSON 程序小结?????var?ds?=?new?Ext.data.Store({
[转]ExtJs + Struts2 + JSON 程序小结????????????????????????????????????proxy:?new?Ext.data.HttpProxy
[转]ExtJs + Struts2 + JSON 程序小结({url:?'getAllOrder.action',method:'POST'}),//Url很关键,我就是因为没配好这个,POST方法很重要,你可以省略,让你看下错误也行的!耽误了一大堆时间!
[转]ExtJs + Struts2 + JSON 程序小结????????????????????????????????????reader:rd
[转]ExtJs + Struts2 + JSON 程序小结????????????????????????????????});
[转]ExtJs + Struts2 + JSON 程序小结?????ds.load();
[转]ExtJs + Struts2 + JSON 程序小结?????var?sm?=new?xg.CheckboxSelectionModel();?//CheckBox选择列
[转]ExtJs + Struts2 + JSON 程序小结?????var?cm?=new?xg.ColumnModel([
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????new?Ext.grid.RowNumberer(),?//行号列?
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????sm,
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????{id:'orderId',header:?"订单号",?dataIndex:?'name'},???????????????????????????{header:?"订单时间",???dataIndex:?'booktime'},
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????{header:?"订单公司",?dataIndex:?'company'},
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????{header:"客户姓名",dataIndex:'customName'}
[转]ExtJs + Struts2 + JSON 程序小结?????????????????????????????????]);
[转]ExtJs + Struts2 + JSON 程序小结?????????????????????????????????cm.defaultSortable?=?true;
[转]ExtJs + Struts2 + JSON 程序小结????////////////////////////////////////////////////////////////////////////////////////////
[转]ExtJs + Struts2 + JSON 程序小结????//?OrderGrid?
[转]ExtJs + Struts2 + JSON 程序小结????////////////////////////////////////////////////////////////////////////////////////////
[转]ExtJs + Struts2 + JSON 程序小结
[转]ExtJs + Struts2 + JSON 程序小结????var?ordergrid?=?new?xg.GridPanel({
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????ds:?ds,
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????sm:?sm,?
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????cm:?cm,?
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????width:1000,
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????height:500,
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????frame:true,
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????title:'Framed?with?Checkbox?Selection?and?Horizontal?Scrolling',
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????iconCls:'icon-grid',
[转]ExtJs + Struts2 + JSON 程序小结??????????????????????????????????renderTo:?document.body
[转]ExtJs + Struts2 + JSON 程序小结?????????????????????????????????});
[转]ExtJs + Struts2 + JSON 程序小结????ordergrid.render();
[转]ExtJs + Struts2 + JSON 程序小结
[转]ExtJs + Struts2 + JSON 程序小结});

?

http://javacrazyer.iteye.com/blog/707510#

热点排行