extjs +String2 +Spring 下的分页 以及返回json格式错误的问题
首先,分页 很简单.
1前台extjs数据源,
var shipMgrStore = Ext.create('Ext.data.Store', { model: 'App.ShipMgr.model.ShipModel', autoLoad: true, pageSize: 15, // timeout: 300000, // simpleSortMode:true, proxy: { type: 'ajax', url:'<%=request.getContextPath()%>/getDuncanJiang.action', reader: { reader: 'json', root: 'sfdcConfigList', totalProperty: 'totalProperty' }, }, });
其他的属性就不说了.
主要是两个参数:一个是 root :表示接收到的数据从这个 sfdcConfigLIst 开始解析
一个是 totalProperty :表示返回数据的总行数
2 grid
var shipMgrGrid = Ext.create('Ext.grid.Panel', { store: shipMgrStore, columnLines: true, rowLines: true, region: 'center', autoScroll: true, forceFit: true, loadMask: true, border: true, renderTo:'showPanel', bbar: Ext.create('Ext.toolbar.Paging', { id:'<portlet:namespace />bbar', store: shipMgrStore, pageSize:15, displayInfo: true }), columns: [ Ext.create('Ext.grid.RowNumberer', { text: 'numbr', width: 50 }), { text: 'id', dataIndex: 'id' } , { text: 'plant', dataIndex: 'plant' }, { text: 'deviceId', dataIndex: 'deviceId' }, { text: 'sfdcPort', dataIndex: 'sfdcPort' }, { text: 'description', dataIndex: 'description' } ], bbar: Ext.create('Ext.toolbar.Paging', { id: '<portlet:namespace />duncanjiangbbar', pageSize: 15, store: shipMgrStore, displayInfo: true, displayMsg: '<%= bundle.getString("message.records") %> {0} - {1} of {2}', emptyMsg: '<%= bundle.getString("page.nodata") %>' }) });
传递二个参数 limit : 每页显示多少条 (pageSize)
start :从第几条数据开始 (自动传递)
当你在前台点击下一页的时候,它就会自动把 limit 和 start 传到后台查询数据,然后返回对面页的数据过来
后台的话
private int start;
//get set
private int limit;
//get set
public String findSfdcByDeviceId(){ try { int limit=this.limit; int start=this.start;
// 根据传过来的参数,查询数据 this.fileNameList=testService.getSfdcCOnfigObjbyDeviceId(type,start,limit); //总条数 totalProperty=testService.getSfdcCOunt(type); }
其实就是 返回 两个数据集,一个是 fileNameList,当页数据, totalPropery是总共有多少条数据.
定义属性接收前台传过来的 limit,start,并且生成get,set方法
&&&&&&&&&&&&&&&我是傲娇的分割线&&&&&&&&&&&&&&&&&&&&&&&
然后做这个分页的时候还遇上一个问题,返回的 JSON数据到前台的时候报了一个错
html><head><title>Apache Tomcat/7.0.39 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.IllegalAccessException: Class org.apache.struts2.json.JSONWriter can not access a member of class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper with modifiers "public"</h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u>org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.IllegalAccessException: Class org.apache.struts2.json.JSONWriter can not access a member of class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper with modifiers "public"</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.IllegalAccessException: Class org.apache.struts2.json.JSONWriter can not access a member of class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper with modifiers "public" org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:243) org.apache.struts2.json.JSONWriter.process(JSONWriter.java:165) org.apache.struts2.json.JSONWriter.value(JSONWriter.java:131) org.apache.struts2.json.JSONWriter.write(JSONWriter.java:99) org.apache.struts2.json.JSONUtil.serialize(JSONUtil.java:112) org.apache.struts2.json.JSONResult.execute(JSONResult.java:198) com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266) com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252) org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
这个是由于后台返回前台的数据中,会包含所有的get开头的,包括你的get属性,get开头的方法等等等等.导致的问题.
解决 : 1.在这些不需要返回的get方法的前面加上@JSON(serialize=false)这个注解.
2.把查询返回的数据集做一个简单的处理,通过gson进行解析.具体为什么要这样....我也不清楚..... 哈哈哈哈,反正这样就可以了
public String findSfdcByDeviceId(){ try { int limit=this.limit; int start=this.start; this.fileNameList=testService.getSfdcCOnfigObjbyDeviceId(type,start,limit); Gson gson=new Gson(); List<SfdcConfigObj> rs=new ArrayList<SfdcConfigObj>(); String str=JSONArray.fromObject(fileNameList).toString(); Type ortype = new TypeToken<ArrayList<SfdcConfigObj>>() {}.getType(); rs=gson.fromJson(str, ortype); for(SfdcConfigObj sfdc : rs){ sfdcConfigList.add(sfdc); } totalProperty=testService.getSfdcCOunt(type); } catch (Exception e) { e.printStackTrace(); } return Action.SUCCESS; }
注意!!! 如果你要返回的,比如一个属性,暂且叫他为 number.这时候他的get方法如果你也加了这个注解的话,那么前台是获取不到这个值的.