Hibernate和jQuery插件flexgrid集成分页

来源:互联网 发布:马自达cx5 知乎 编辑:程序博客网 时间:2024/05/02 14:03

Hibernate和jQuery插件flexgrid集成分页

 

1.在Struts的Action中这样写:

package com.hithd.iswmanagement.Actions.StaffAction;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import com.hithd.iswmanagement.Services.ShowStaffService;/** * 显示职工信息Action. * @author Administrator * */public class ShowStaffAction extends Action { private ShowStaffService showStaffService; public void setShowStaffService(ShowStaffService showStaffService) {  this.showStaffService = showStaffService; } @Override public ActionForward execute(ActionMapping mapping, ActionForm form,   HttpServletRequest request, HttpServletResponse response)   throws Exception {  /* 从前台页面取得当页的页号. */  int pageNo = Integer.parseInt(request.getParameter("pageNo"));  /* 定义每页显示的记录数. */  int pageSize = 5;  /* Hibernate内置分页的起始记录号. */  int firstResult = 0;  /* Hibernate内置分页的终止记录号. */  int maxResult = pageSize;  /* 从前台拿到提交的按钮状态,用来判断分页操作. */  String buttonstate = request.getParameter("buttonstate");  /* 计算出总共有多少页. */  List allstaffList = this.showStaffService.ShowAllStaff();  int totalRecald = allstaffList.size();  int totalPage = totalRecald / pageSize;  if ((totalRecald % pageSize) > 0) {   totalPage = totalRecald / pageSize + 1;  }  /* 前台提交的是首页. */  if (buttonstate.equals("First")) {   pageNo = 1;  }  /* 前台提交的是尾页. */  if (buttonstate.equals("Last")) {   pageNo = totalPage;  }          /* 当页面已经到首页. */  if (pageNo < 1) {   pageNo = 1;  }       /* 当前页已经倒尾页. */  if (pageNo > totalPage) {   pageNo = totalPage;  }  /* 当分页时所有的页号必须从1开始. */  if (pageNo >= 1) {   /* 提交的是下一页. */   firstResult = (pageNo - 1) * pageSize;   /* 将从数据库中取出的职工信息存放在staffList中. */   List staffList = this.showStaffService.ShowStaff(firstResult,     maxResult);   request.setAttribute("staffList", staffList);   /* 当前页,放到内置对象里,前台要用到. */   request.setAttribute("pageNo", pageNo);  }  /* 前台要显示总页数和当前页. */  request.setAttribute("totalPage", totalPage);  return mapping.findForward("success");  // TODO Auto-generated method stub }}


 

2.在有flexgrid的页面中这样写:

//flaxgrid上的按钮的处理函数,分页等    function test1(data){   //alert('my' + data);      if(data == 'Next') {    $("#staffForm").attr("action","ShowStaffAction.do?buttonstate=Next&pageNo=${pageNo+1}");  //找到staffForm对应的form表单,并设置它的action属性为删除的action       $("#staffForm").submit();                             //提交操作   }   if(data == 'Reload') {    $("#staffForm").attr("action","ShowStaffAction.do?buttonstate=Reload&pageNo=${pageNo}");  //找到staffForm对应的form表单,并设置它的action属性为删除的action       $("#staffForm").submit();                             //提交操作   }   if(data == 'Prev') {    $("#staffForm").attr("action","ShowStaffAction.do?buttonstate=Prev&pageNo=${pageNo-1}");  //找到staffForm对应的form表单,并设置它的action属性为删除的action       $("#staffForm").submit();                             //提交操作   }   if(data == 'First') {    $("#staffForm").attr("action","ShowStaffAction.do?buttonstate=First&pageNo=${pageNo-1}");  //找到staffForm对应的form表单,并设置它的action属性为删除的action       $("#staffForm").submit();                             //提交操作   }   if(data == 'Last') {    $("#staffForm").attr("action","ShowStaffAction.do?buttonstate=Last&pageNo=${pageNo+1}");  //找到staffForm对应的form表单,并设置它的action属性为删除的action       $("#staffForm").submit();                             //提交操作   }  }


 

 

3.在业务层Dao这样写:

package com.hithd.iswmanagement.DaosImpl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.hithd.iswmanagement.Daos.ShowStaffDao;public class ShowStaffDaoImpl extends HibernateDaoSupport implements  ShowStaffDao { public List ShowStaff(int firstResult, int maxResult) {/* 用Query接口实现查询,并把所有数据存到一个LIST中. */  List staffList = this.getSession().createQuery("from StaffInformation")    .setFirstResult(firstResult).setMaxResults(maxResult).list();  return staffList;  // TODO Auto-generated method stub } public List ShowAllStaff() {  List allstaffList = this.getSession().createQuery(    "from StaffInformation").list();  return allstaffList; }}


 

4.分页效果: