ExtJs + Struts2 + JSON
最近一直都在看EXTJS的东西,然后自己实践了下,界面倒是蛮漂亮的,但是一旦涉及到与服务器端进行数据互动麻烦就出来了,本来下了个例子确发现是 用DWR的,觉得我既然用了STRUTS2作为MVC的框架,我觉得这个框架还是很不错的,觉得还是把EXTJS整合到一起更好些,找了相关的资料,跟着 前辈做了下例子,发现完全不是那么回事,只好自己慢慢摸索,终于把数据交互的问题解决了,所以记录之以便查阅!
还是从底层开始说吧,拿最经典的例子来解说吧,订单和客户的关系显然是n:1的关系,我hibernate不是用的声明方式所以就用的xml方式做的那么相应的hbm.xml文件如下:
ORDER.XML
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.model.Order" table="t_order" lazy="false">
<id name="orderId" column="OrderId">
<generator class="uuid.hex" />
</id>
<property name="name" column="Name" type="string" />
<property name="desn" column="Desn" type="string"/>
<property name="booktime" column="Booktime" type="string"/>
<property name="company" column="Company" />
<many-to-one lazy="false" name="custom" column="CustomId" class="com.model.Customer" />
</class>
</hibernate-mapping>
CUSTOM.XML
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.model.Custom" table="t_custom" lazy="false">
<id name="customId" column="Id">
<generator class="uuid.hex" />
</id>
<property name="customName" column="Name" type="string" />
</class>
</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方法如下:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import net.sf.json.*;
//具体的那些serivce的包引入我就省略了
public class OrderAction extends ActionSupport
{
private static final long serialVersionUID = -5092865658281004791L;
private IOrderSerivce orderSerivce;
private String jsonString;//这个就是中转站了
private List<Order> orderList;//这个是数据链表
private int totalCount;//这个是extjs用来分页
public String getJsonString()
{
return jsonString;
}
public void setJsonString(String jsonString)
{
this.jsonString = jsonString;
}
public int getTotalCount()
{
return totalCount;
}
public void setTotalCount(int totalCount)
{
this.totalCount = totalCount;
}
public List<Air> getOrderList()
{
return orderList;
}
public void setOrderList(List<Order> orderList)
{
this.orderList = orderList;
}
public void setOrderSerivce(OrderSerivce orderSerivce)
{
this.orderSerivce = orderSerivce;
}
public String getAllAir()
{
orderList = orderSerivce.getOrderAll();
this.setTotalCount(orderList.size()); JSONArray array = JSONArray.fromObject(orderList);
//哈哈,就是在这里进行转换的
this.jsonString = "{totalCount:"+this.getTotalCount()+",results:"+array.toString()+"}";
return SUCCESS;
}
}
接下来再是什么,哦,是的,应该是STRUTS的配置了,哈哈
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="order" extends="struts-default">
<action name="getAllOrder" class="orderAction" method="getAllOrder">
<result name="success" >jsondata.jsp</result>
</action>
</package>
</struts>
好的,看到jsondata.jsp了么,这里就是要放数据的地方,看看是什么吧!
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:property value="jsonString" escape="false" />
是的,就是这么简单的一个代码!终于要到前台了,该露脸了,呵呵,前台代码最关键的也就是JS代码,那么我也就只贴JS了相信大家看过后都会自己弄清楚的!
/*
* Ext JS Library 2.1
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/ Ext.onReady(function(){
Ext.BLANK_IMAGE_URL = 'ext/resources/images/default/s.gif';
Ext.QuickTips.init();
var xg = Ext.grid;
//这里就是设置解析格式的地方,一定要和你的Model一样,要不然可是什么都得不到哦~~~~
var rd = new Ext.data.JsonReader({
//总记录数
totalProperty: 'totalCount',
//哪儿是数据的头,可以看action里面是怎么定义数据格式的,这里就是如何解析的
root: 'results',
//有那些字段呢?
fields:[
{name:'orderId'},
{name:'desn'},
{name:'booktime'},
{name:'company'},
{name:'name'},
//这里就是对custom对象进行映射的地方
{name:'customId' ,mapping:'custom.customId'},
{name:'customName',mapping:'custom.customName'}
]
});
var ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy
({url: 'getAllOrder.action',method:'POST'}),//Url很关键,我就是因为没配好这个,POST方法很重要,你可以省略,让你看下错误也行的!耽误了一大堆时间!
reader:rd
});
ds.load();
var sm =new xg.CheckboxSelectionModel(); //CheckBox选择列
var cm =new xg.ColumnModel([
new Ext.grid.RowNumberer(), //行号列
sm,
{id:'orderId',header: "订单号", dataIndex: 'name'}, {header: "订单时间", dataIndex: 'booktime'},
{header: "订单公司", dataIndex: 'company'},
{header:"客户姓名",dataIndex:'customName'}
]);
cm.defaultSortable = true;
////////////////////////////////////////////////////////////////////////////////////////
// OrderGrid
//////////////////////////////////////////////////////////////////////////////////////// var ordergrid = new xg.GridPanel({
ds: ds,
sm: sm,
cm: cm,
width:,
height:,
frame:true,
title:'Framed with Checkbox Selection and Horizontal Scrolling',
iconCls:'icon-grid',
renderTo: document.body
});
ordergrid.render(); });
ExtJs + Struts2 + JSON的更多相关文章
- struts2 java.lang.StackOverflowError org.apache.struts2.json.JSONWriter
1. 问题描述: 页面通过异步访问action, action的方法通过map封装数据,struts的result的type设置为json,后台报错 六月 25, 2016 6:54:33 下午 ...
- Class org.apache.struts2.json.JSONWriter can not access a member of class org.springframework.aop.TruePointcut with modifiers "public"
Spring注入Action使用Json错误:org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: ...
- org.apache.struts2.json.JSONWriter can not access a member of class
偶遇一个问题:org.apache.struts2.json.JSONWriter can not access a member of class org.apache.tomcat.dbcp.db ...
- Flex+Struts2+JSON实现Flex和后台的HTTP Service请求
http://www.fengfly.com/plus/view-191093-1.html Flex+Struts2+JSON的后台代码我在这就不多说了.不懂得请看我写的上一篇文章<Strut ...
- Jqgrid入门-结合Struts2+json实现数据展示(五)
DEMO用的是ssh框架实现的,具体怎么搭建的就不多做说明了.分页表格的数据操作难点就是数据展现.至于增删改直接用hibernate原生的方法实现即可. 初步分析:表格要实现分页,那么 ...
- Class org.apache.struts2.json.JSONWriter can not access a member of
异常形式: Class org.apache.struts2.json.JSONWriter can not access a member of * 或是 Class com.googlecode. ...
- Android+struts2+JSON方式的手机开发(Login)
在手机的后台服务无论是调用WebService还是Http请求,多数都是采用Android的HttpClient实现相关的调用实现.本文实现Android+Struts2+JSON方式实现为手机前台提 ...
- struts2 json 定义全局Date格式
使用struts2的json插件时,自己定义日期格式经常使用的方式是在get属性上加入@JSON注解,这个对于少量Date属性还能够,可是假设date字段多了,总不可能去给每一个date get方法加 ...
- Class org.apache.struts2.json.JSONWriter can not access a member of class oracle.jdbc.driver.Physica
产生这个错误的原因是因为我的oracle数据库中有一个CLOB字段,查询出来的时候要转换为JSON而报错. Class org.apache.struts2.json.JSONWriter can n ...
随机推荐
- CentOS 6.3 源码安装LAMP(Linux+Apache+Mysql+Php)环境
一.简介 什么是LAMP LAMP是一种Web网络应用和开发环境,是Linux, Apache, MySQL, Php/Perl的缩写,每一个字母代表了一个组件,每个组件就其本身而>言都是在它所 ...
- [转] 让ctags支持Javascript
mac下安装exuberant ctags mac 下自带ctags但是功能有限,要使用一些常用的功能需要安装exuberant ctags 下载exuberant ctags 安装exuberant ...
- EF结合SqlBulkCopy在项目中的使用
这是我第一次写博客,由于水平有限,写不出什么好东西,还望见谅. 我现在参与的这个项目采用的是EF框架,方便了数据库的访问.但在实际中,发现项目中导入市县Excel数据耗时太长,于是趁这段时间专门研究了 ...
- wpf 大控件 打印 将控件转成 xps格式 并分页打印
//PayRollPrintList:要打印的 list 可换成自己要打印的类型 private List<PayRoll> _PayRollPrintList = new List< ...
- JS & JQuery 动态添加 select option
因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...
- PAT - 基础 - 龟兔赛跑
题目: 乌龟与兔子进行赛跑,跑场是一个矩型跑道,跑道边可以随地进行休息.乌龟每分钟可以前进3米,兔子每分钟前进9米:兔子嫌乌龟跑得慢,觉得肯定能跑赢乌龟,于是,每跑10分钟回头看一下乌龟,若发现自己超 ...
- jquery插件dataTables自增序号。
dataTables官网提供了一种方式,使用后没有达到预期效果(js报错),没有深究原因.如果需要,可以按照下面的方式来. $('#dataList').dataTable({ "langu ...
- FileReader上传图片
实现拖拽图片,在上传至服务器前,显示图片并操控大小 利用HTML5 dragenter dragover dragleave drop 在实现图片显示方面,用了FileReader这个类 var fi ...
- Wdcp两日志的路径
Wdcp两日志的路径: /www/wdlinux/httpd-2.2.22/logs /www/wdlinux/nginx-1.0.15/logs
- Nginx fastcgi_param解释
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径 fastcgi_param QUERY_STRI ...