前台页面用extjs来实现分页,在准备数据阶段,
//定义Proxy
var proxy = new Ext.data.HttpProxy({url:"<%=basePath%>login/workerAction!selectAllPersonInfo"});//从远程服务器获取数据
//定义Reader
var Human = new Ext.data.Record.create([
{name:"Name",type:"string",mapping:"name"},
{name:"Sex",type:"string",mapping:"sex"},
{name:"Birthday",type:"string",mapping:"birthday"},
{name:"Edu",type:"string",mapping:"edu"},
{name:"Dept",type:"string",mapping:"dept"},
{name:"Memo",type:"string",mapping:"memo"}
]);
var reader = new Ext.data.JsonReader(
{totalProperty:"totalProperty",root:"root"},
Human
);
//定义Store
var store = new Ext.data.Store({
proxy:proxy,
reader:reader
});
store.load({params:{start:0,limit:6}}); //第一次加载的时候,起始是0,每6条一页
//定义分页工具栏
var bbar = new Ext.PagingToolbar({
store:store,
pageSize:6, //页大小
displayInfo:true, //是否显示displayMsg信息
displayMsg:"本页显示第{0}到第{1}的信息,一共{2}记录",
emptyMsg:"没有记录" //如果没有记录就显示该文本信息
});
然后利用hibernate来传递两个参数:
//进行分页查询,查询出当前页内的员工信息
public List<Worker> selectWorkersByPage(int start,int limit){
List<Worker> list = new ArrayList<Worker>();
Session session = super.getSession(); //得到Session
String sql = "from Worker"; //hql语句
Query q = session.createQuery(sql);
q.setFirstResult(start); //起始页
q.setMaxResults(limit); //每页显示的最大条数
list = q.list();
super.releaseSession(session); //释放Session
return list;
}
在Action中调用Hibernate中的方法:
//查询出所有的员工信息
public void selectAllPersonInfo() throws IOException{
response.setContentType("text/html;charset=utf8");
PrintWriter out = response.getWriter();
List<Worker> workList = new ArrayList<Worker>();
List<Human> humanList = new ArrayList<Human>();
List<Human> pageList = new ArrayList<Human>();
int start = Integer.parseInt(request.getParameter("start")); //得到起始和每页显示的数据
int limit = Integer.parseInt(request.getParameter("limit"));
workList = this.workerService.selectWorkersByPage(start,limit); //查询出所有的员工信息
int totalProperty = this.workerService.selectAllWorker().size(); //总条数
Iterator workIt = workList.iterator();
while(workIt.hasNext()){
Human human = new Human();
Worker worker = (Worker)workIt.next();
human.setHid(worker.getId());
human.setName(worker.getName());
human.setSex(worker.getSex());
human.setBirthday(worker.getBirthday());
human.setDept(worker.getDept());
human.setEdu(worker.getEdu());
human.setMemo(worker.getMemo());
humanList.add(human);
}
//定义分页所需要的格式
PageBean pageBean = new PageBean(totalProperty,humanList);
JSONObject jObject = JSONObject.fromObject(pageBean,JsonUtil.configJson("yyyy-MM-dd"));
out.print(jObject.toString());
out.flush();
out.close();
}