我使用的是struts2
extjs代码:
----------------------------------------------------------------code------------------------------------------------------
var store = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url:'ListUser.action'
}),
reader:new Ext.data.JsonReader({
root : 'list',
totalProperty : 'totalCount',
id : 'id',
successProperty : '@success'
},
[ {name : 'id',mapping : 'id',type : 'int'},
{name : 'password',mapping : 'password'},
{name : 'name',mapping : 'name'},
{name : 'age',mapping : 'age'}
])
});
store.load({params:{start:0,limit:10}}); (这个很重要,action会用到)
----------------------------------------------------------------code------------------------------------------------------
action代码:
----------------------------------------------------------------code------------------------------------------------------
private int totalCount = 0;
private transient int start = 0;
private transient int limit = 0;
private String jsonString = "";
...
public String ListUser() throws Exception{
List(User) userlist 为像后台请求的数据
JSONArray array = null;
try {
//使用JSONArray将userlist转换为json对象
array = JSONArray.fromObject(this.userlist);
} catch (Exception e) {
logger.info("JSONArray.fromObject()转化异常:"+e.getMessage());
e.printStackTrace();
}
int start = this.getStart();//前台传过来的
int limit = this.getLimit();//前台传过来的
int total = this.getTotalCount();//就是userlist.size()
if(start + limit >= total){//设置最后一页数据小于limit时
limit = total-start;
}
int sum = start + limit;
StringBuffer sb = new StringBuffer();//其实也可用String,一页也就那么几条 效率差不多
for(int i = start ;i < sum; i++)
{
sb.append(array.get(i).toString());//将得到的数据拼成{...},{...},{...}
if (i != sum - 1)
sb.append(",");
}
this.setJsonString("{success:true,totalCount : " + this.getTotalCount()
+ ", list:[" +
sb
+"]}");
System.out.println(this.getJsonString());//{success:true,totalCount : 21, list:[{"age":32,"id":54,"name":"dd","password":"222"}]}
} catch (Exception e) {
e.printStackTrace();
}
return this.jsonExecute();
}