一直以为extjs前台可以分页(假分页) 我们取出所有数据,返回它总条数,然后 pagingtoolbar实现前台控制分页,但是不能实现。不知是否是我未调试出。下面介绍Extjs与hibernate3交互实现后台分页。
首先前台我们需要设置一个数据中心 store
var studentstore = Ext.create('Ext.data.Store',
{ fields:
[
'stuid',
'stuname',
's_id',
'stupassword',
'tel',
'qqnum'
],
pageSize:2,//注意这里必须设置页面条数,不然pagingtoolbar不会实现分页,会出现这个效果http://bbs.csdn.net/topics/360172987
proxy:
{
type:'ajax',
url:'viewstudent',//action后台地址名
reader:{
type:'json',
root:'users',//对应struts.xml里面root
totalProperty:'totalCount',
id:'stuid'}
},
autoLoad:{ params: { start: 0, limit: 2} }
})
设置grid和pagingtoolbar:
var viestudent=new Ext.grid.Panel({
title: '学生信息查询',
name:'student',
store: studentstore,
columns:[{
header:'学号',
dataIndex:'stuid'
},
{
header:'姓名',
dataIndex:'stuname'
},
{
header:'编号',
dataIndex:'s_id'
},
{
header:'电话号码',
dataIndex:'tel'
},
{
header:'QQ号码',
dataIndex:'qqnum'
},
{
header:'密码',
dataIndex:'stupassword'
}
],
autowidth: true,
autoHeight:true,
loadMask: { msg: '拼命加载中....' },
bbar: { xtype: 'pagingtoolbar',
store: studentstore,
pageSize : 2,
displayInfo: true ,
displayMsg:'显示第 {0} 条到 {1} 条记录,一共 {2} 条',
emptyMsg:'没有记录'
/*,
plugins: Ext.create('Ext.ux.SlidingPager', {})*/
},
buttonAlign:'left',
buttons:[{
text:'刷新',
handler:function(){
studentstore.reload();
}
}],
renderTo: Ext.getBody()
});
至此前台工作完成。
进入后台struts.xml:
</action>
<action name="viewstudent" class="Viewstudent">//class对应applicationContext.xml里面的bean
<result name="success" type="json">
<param name="root">aa</param>//对应前面store--》reader--》root(aa只是一个传送json介质)
</result>
</action>
注意 返回的是json对象 ,进入具体action:
package com.MG.action;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import org.apache.struts2.json.annotations.JSON;
import com.MG.entity.User;
import com.MG.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class ViewstudentAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
UserService userService;
@JSON(serialize = false)
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
Map aa;
int limit;
int start;
public void setLimit(int limit) {
this.limit = limit;
}
public void setStart(int start) {
this.start = start;
}
public Map getAa() {
return aa;
}
public void setAa(Map aa) {
this.aa = aa;
}
public String execute()
{
List<User> users=this.userService.findAll();//总条数
List<User> userss=this.userService.findAlls(start, limit);
int totalCount=users.size();
JSONArray array=JSONArray.fromObject(userss);
Map<String,Object> maps=new HashMap<String,Object>();
maps.put("users", array);
maps.put("totalCount", totalCount);
this.setAa(maps);//aa介质
System.out.println(maps);
return SUCCESS;
}
}
注意 引用json jar文件之后 会自动串行里面定义的变量 所以 不需要get方法的可以不要引用 以为出错 不过也可以告诉json不要串行他
@JSON(serialize = false)
public UserService getUserService() {
return userService;
}
进入DAO层:
@SuppressWarnings("unchecked")
@Override
public List<User> findAlls(final int start, final int limit) {
// TODO Auto-generated method stub
return getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query q = session.createQuery("from User u");
q.setFirstResult(start);
q.setMaxResults(limit);
return q.list();
}
});
}
至此即可整合实现分页效果。下面上图: