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

ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2<?xml version="1.0"?>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
<!DOCTYPE hibernate-mapping PUBLIC 
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
> 
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2       
<hibernate-mapping>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2       
<class name="com.model.Order"  table="t_order" lazy="false">
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2           
<id name="orderId" column="OrderId">
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2               
<generator class="uuid.hex" />
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2           
</id>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2           
<property name="name" column="Name" type="string" />
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2           
<property name="desn" column="Desn" type="string"/>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2           
<property name="booktime" column="Booktime" type="string"/>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2           
<property name="company" column="Company" />
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2           
<many-to-one lazy="false" name="custom" column="CustomId" class="com.model.Customer" />
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2       
</class>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2   
</hibernate-mapping>

        CUSTOM.XML
 

ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2<?xml version="1.0"?>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
<!DOCTYPE hibernate-mapping PUBLIC 
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
> 
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2   
<hibernate-mapping>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2       
<class name="com.model.Custom"  table="t_custom" lazy="false">
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2           
<id name="customId" column="Id">
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2               
<generator class="uuid.hex" />
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2           
</id>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2           
<property name="customName" column="Name" type="string" />
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2       
</class>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2   
</hibernate-mapping>


        相应的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 程序总结_EXT JSON STRUTS2import java.text.DateFormat;
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
import java.text.ParseException;
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
import java.text.SimpleDateFormat;
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
import java.util.Date;
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
import java.util.List;
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
import net.sf.json.*;
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
//具体的那些serivce的包引入我就省略了
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
public class OrderAction extends ActionSupport
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_37
{
ExtJs + Struts2 + JSON 程序总结_职场_40   
private static final long serialVersionUID = -5092865658281004791L;
ExtJs + Struts2 + JSON 程序总结_职场_40    
private IOrderSerivce orderSerivce;
ExtJs + Struts2 + JSON 程序总结_职场_40    
private String jsonString;//这个就是中转站了
ExtJs + Struts2 + JSON 程序总结_职场_40
    private List<Order> orderList;//这个是数据链表
ExtJs + Struts2 + JSON 程序总结_职场_40
    private int totalCount;//这个是extjs用来分页
ExtJs + Struts2 + JSON 程序总结_职场_40
     public String getJsonString()
ExtJs + Struts2 + JSON 程序总结_休闲_46    
{
ExtJs + Struts2 + JSON 程序总结_职场_40        
return jsonString;
ExtJs + Struts2 + JSON 程序总结_休闲_50    }

ExtJs + Struts2 + JSON 程序总结_职场_40     
public void setJsonString(String jsonString)
ExtJs + Struts2 + JSON 程序总结_职场_52    
{
ExtJs + Struts2 + JSON 程序总结_职场_40        
this.jsonString = jsonString;
ExtJs + Struts2 + JSON 程序总结_休闲_50    }

ExtJs + Struts2 + JSON 程序总结_职场_40    
public int getTotalCount()
ExtJs + Struts2 + JSON 程序总结_职场_58    
{
ExtJs + Struts2 + JSON 程序总结_职场_40        
return totalCount;
ExtJs + Struts2 + JSON 程序总结_休闲_50    }

ExtJs + Struts2 + JSON 程序总结_职场_40    
public void setTotalCount(int totalCount)
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_64    
{
ExtJs + Struts2 + JSON 程序总结_职场_40        
this.totalCount = totalCount;
ExtJs + Struts2 + JSON 程序总结_休闲_50    }

ExtJs + Struts2 + JSON 程序总结_职场_40    
public List<Air> getOrderList()
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_70    
{
ExtJs + Struts2 + JSON 程序总结_职场_40        
return orderList;
ExtJs + Struts2 + JSON 程序总结_休闲_50    }

ExtJs + Struts2 + JSON 程序总结_职场_40    
public void setOrderList(List<Order> orderList)
ExtJs + Struts2 + JSON 程序总结_职场_76    
{
ExtJs + Struts2 + JSON 程序总结_职场_40        
this.orderList = orderList;
ExtJs + Struts2 + JSON 程序总结_休闲_50    }

ExtJs + Struts2 + JSON 程序总结_职场_40    
public void setOrderSerivce(OrderSerivce orderSerivce)
ExtJs + Struts2 + JSON 程序总结_休闲_82    
{
ExtJs + Struts2 + JSON 程序总结_职场_40        
this.orderSerivce = orderSerivce;
ExtJs + Struts2 + JSON 程序总结_休闲_50    }

ExtJs + Struts2 + JSON 程序总结_职场_40    
public String getAllAir()
ExtJs + Struts2 + JSON 程序总结_职场_88    
{
ExtJs + Struts2 + JSON 程序总结_职场_40        orderList 
= orderSerivce.getOrderAll();
ExtJs + Struts2 + JSON 程序总结_职场_40        
this.setTotalCount(orderList.size());
ExtJs + Struts2 + JSON 程序总结_职场_40        
ExtJs + Struts2 + JSON 程序总结_职场_40        JSONArray array 
= JSONArray.fromObject(orderList);
ExtJs + Struts2 + JSON 程序总结_职场_40
//哈哈,就是在这里进行转换的
ExtJs + Struts2 + JSON 程序总结_职场_40
        this.jsonString = "{totalCount:"+this.getTotalCount()+",results:"+array.toString()+"}";
ExtJs + Struts2 + JSON 程序总结_职场_40    
return SUCCESS;
ExtJs + Struts2 + JSON 程序总结_休闲_50    }

ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_99}

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

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

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

ExtJs + Struts2 + JSON 程序总结_职场_111<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_114
<%@ taglib prefix="s" uri="/struts-tags" %>
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
<s:property value="jsonString" escape="false" />

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

ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_118/*
ExtJs + Struts2 + JSON 程序总结_职场_40 * Ext JS Library 2.1
ExtJs + Struts2 + JSON 程序总结_职场_40 * Copyright(c) 2006-2008, Ext JS, LLC.
ExtJs + Struts2 + JSON 程序总结_职场_40 * licensing@extjs.com
ExtJs + Struts2 + JSON 程序总结_职场_40 * 
ExtJs + Struts2 + JSON 程序总结_职场_40 * http://extjs.com/license
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_99 
*/

ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2
ExtJs + Struts2 + JSON 程序总结_休闲_128Ext.onReady(
function(){
ExtJs + Struts2 + JSON 程序总结_职场_40    Ext.BLANK_IMAGE_URL 
= 'ext/resources/p_w_picpaths/default/s.gif'; 
ExtJs + Struts2 + JSON 程序总结_职场_40    Ext.QuickTips.init();
ExtJs + Struts2 + JSON 程序总结_职场_40    
var xg = Ext.grid;
ExtJs + Struts2 + JSON 程序总结_职场_40    
//这里就是设置解析格式的地方,一定要和你的Model一样,要不然可是什么都得不到哦~~~~
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_135
    var rd = new Ext.data.JsonReader({
ExtJs + Struts2 + JSON 程序总结_职场_40                
//总记录数
ExtJs + Struts2 + JSON 程序总结_职场_40
                totalProperty: 'totalCount', 
ExtJs + Struts2 + JSON 程序总结_职场_40               
//哪儿是数据的头,可以看action里面是怎么定义数据格式的,这里就是如何解析的           
                    root: 'results', 

ExtJs + Struts2 + JSON 程序总结_职场_40
                //有那些字段呢?
ExtJs + Struts2 + JSON 程序总结_职场_40
                fields:[
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_143                         
{name:'orderId'},
                                      {name:'desn'},
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_146                         
{name:'booktime'},
ExtJs + Struts2 + JSON 程序总结_职场_149                         
{name:'company'},
ExtJs + Struts2 + JSON 程序总结_休闲_152                         
{name:'name'},
ExtJs + Struts2 + JSON 程序总结_职场_40                            
//这里就是对custom对象进行映射的地方                        
                                      {name:'customId' ,mapping:'custom.customId'},
ExtJs + Struts2 + JSON 程序总结_休闲_156                         {name:'customName',mapping:'custom.customName'}
ExtJs + Struts2 + JSON 程序总结_职场_40                         ]
ExtJs + Struts2 + JSON 程序总结_休闲_50                                     }
);
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_161     
var ds = new Ext.data.Store({
ExtJs + Struts2 + JSON 程序总结_职场_40                                    proxy: 
new Ext.data.HttpProxy
ExtJs + Struts2 + JSON 程序总结_职场_165(
{url: 'getAllOrder.action',method:'POST'}),//Url很关键,我就是因为没配好这个,POST方法很重要,你可以省略,让你看下错误也行的!耽误了一大堆时间!
ExtJs + Struts2 + JSON 程序总结_职场_40
                                    reader:rd
ExtJs + Struts2 + JSON 程序总结_休闲_50                                }
);
ExtJs + Struts2 + JSON 程序总结_职场_40     ds.load();
ExtJs + Struts2 + JSON 程序总结_职场_40     
var sm =new xg.CheckboxSelectionModel(); //CheckBox选择列
ExtJs + Struts2 + JSON 程序总结_职场_40
     var cm =new xg.ColumnModel([
ExtJs + Struts2 + JSON 程序总结_职场_40                                  
new Ext.grid.RowNumberer(), //行号列 
ExtJs + Struts2 + JSON 程序总结_职场_40
                                  sm,
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_175                                  
{id:'orderId',header: "订单号", dataIndex: 'name'},                           {header: "订单时间",   dataIndex: 'booktime'},
ExtJs + Struts2 + JSON 程序总结_职场_179                                  
{header: "订单公司", dataIndex: 'company'},
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_182                                  
{header:"客户姓名",dataIndex:'customName'}
ExtJs + Struts2 + JSON 程序总结_职场_40                                 ]);
ExtJs + Struts2 + JSON 程序总结_职场_40                                 cm.defaultSortable 
= true;
ExtJs + Struts2 + JSON 程序总结_职场_40    
////////////////////////////////////////////////////////////////////////////////////////
ExtJs + Struts2 + JSON 程序总结_职场_40
    // OrderGrid 
ExtJs + Struts2 + JSON 程序总结_职场_40
    ////////////////////////////////////////////////////////////////////////////////////////
ExtJs + Struts2 + JSON 程序总结_职场_40

ExtJs + Struts2 + JSON 程序总结_职场_191    
var ordergrid = new xg.GridPanel({
ExtJs + Struts2 + JSON 程序总结_职场_40                                  ds: ds,
ExtJs + Struts2 + JSON 程序总结_职场_40                                  sm: sm, 
ExtJs + Struts2 + JSON 程序总结_职场_40                                  cm: cm, 
ExtJs + Struts2 + JSON 程序总结_职场_40                                  width:
1000,
ExtJs + Struts2 + JSON 程序总结_职场_40                                  height:
500,
ExtJs + Struts2 + JSON 程序总结_职场_40                                  frame:
true,
ExtJs + Struts2 + JSON 程序总结_职场_40                                  title:'Framed 
with Checkbox Selection and Horizontal Scrolling',
ExtJs + Struts2 + JSON 程序总结_职场_40                                  iconCls:'icon
-grid',
ExtJs + Struts2 + JSON 程序总结_职场_40                                  renderTo: document.body
ExtJs + Struts2 + JSON 程序总结_休闲_50                                 }
);
ExtJs + Struts2 + JSON 程序总结_职场_40    ordergrid.render();
ExtJs + Struts2 + JSON 程序总结_职场_40
ExtJs + Struts2 + JSON 程序总结_EXT JSON STRUTS2_99}
);