1.定义一个page类,里面包括每一个的数量,当前页码,总记录数,等
2.将page对象传入dao中,dao输出分页查询好的数据。返回到action中,action放到request中,jsp从request中获取
3.jsp传一个curPageNum到action中,action设置好page对象,传给dao查询
page类
1 /** 2 * 用于分页显示 3 * @author GXF 4 * 5 */ 6 public class Page { 7 private int pageSize = 10; //每一页显示大小,默认为10 8 private int totalNum; //记录总数 9 private int pageNums; //一共有多少页 10 private int curPageNum; //当前页页号 11 12 public int getPageSize() { 13 return pageSize; 14 } 15 public void setPageSize(int pageSize) { 16 this.pageSize = pageSize; 17 } 18 public int getTotalNum() { 19 return totalNum; 20 } 21 public void setTotalNum(int totalNum) { 22 this.totalNum = totalNum; 23 } 24 public int getPageNums() { 25 return pageNums; 26 } 27 public void setPageNums(int pageNums) { 28 this.pageNums = pageNums; 29 } 30 public int getCurPageNum() { 31 return curPageNum; 32 } 33 public void setCurPageNum(int curPageNum) { 34 this.curPageNum = curPageNum; 35 } 36 37 38 39 }
Dao类的方法
1 /** 2 * 分页查询 3 * @param page 4 * @return 5 */ 6 public List<Employee> queryEmployeeByPage(Page page){ 7 int total = getEmployeeTotalCount(); 8 page.setTotalNum(total); //设置page总数 9 10 //设置页数 11 int pageNum = total / page.getPageSize(); 12 if(total % page.getPageSize() != 0) 13 pageNum++; 14 page.setPageNums(pageNum); 15 16 //开始查询的记录 17 int start = page.getCurPageNum() * page.getPageSize(); 18 //显示最后一页内容 19 if(start >= total){ 20 start = (page.getPageNums() - 1) * page.getPageSize(); 21 } 22 23 //开始查询 24 List<Employee> listOfEmployee = new ArrayList<Employee>(); 25 Session session = baseDao.getSession(); 26 session.beginTransaction(); 27 String hql = "from Employee"; 28 Query query = session.createQuery(hql); 29 query.setFirstResult(start); 30 query.setMaxResults(page.getPageSize()); 31 listOfEmployee = query.list(); 32 session.getTransaction().commit(); 33 34 session.close(); 35 36 return listOfEmployee; 37 38 }
action类
1 import java.util.List; 2 import java.util.Map; 3 4 import org.apache.struts2.interceptor.RequestAware; 5 6 import com.gxf.IDao.IEmployeeDao; 7 import com.gxf.entity.Employee; 8 import com.gxf.util.Page; 9 import com.opensymphony.xwork2.ActionSupport; 10 11 /** 12 * 员工分页显示 13 * @author GXF 14 * 15 */ 16 public class EmployeeAction extends ActionSupport implements RequestAware{ 17 private Map<String, Object> request; 18 private IEmployeeDao employeeDao; 19 private Page page = new Page(); 20 private int curPageNumber; 21 22 /** 23 * 24 */ 25 private static final long serialVersionUID = 1L; 26 27 public String queryAllEmployee(){ 28 //设置好当前页码 29 page.setCurPageNum(curPageNumber); 30 31 List<Employee> listOfEmployee = employeeDao.queryEmployeeByPage(page); 32 if(curPageNumber <= 0) 33 curPageNumber = 0; 34 if(curPageNumber >= page.getPageNums()) 35 curPageNumber = page.getPageNums(); 36 request.put("listOfEmployee", listOfEmployee); 37 for(int i = 0; i < listOfEmployee.size(); i++) 38 System.out.println(listOfEmployee.get(i).getName()); 39 return SUCCESS; 40 } 41 42 @Override 43 public void setRequest(Map<String, Object> request) { 44 this.request = request; 45 } 46 47 public IEmployeeDao getEmployeeDao() { 48 return employeeDao; 49 } 50 51 public void setEmployeeDao(IEmployeeDao employeeDao) { 52 this.employeeDao = employeeDao; 53 } 54 55 public Page getPage() { 56 return page; 57 } 58 59 public void setPage(Page page) { 60 this.page = page; 61 } 62 63 public int getCurPageNumber() { 64 return curPageNumber; 65 } 66 67 public void setCurPageNumber(int curPageNumber) { 68 this.curPageNumber = curPageNumber; 69 } 70 71 }
JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>employee分页显示实现</title> </head> <body> employeeList分页显示<br> <form action="#"> <s:iterator value="#request.listOfEmployee" id="employee"> <s:property value="#employee.name"/><br> </s:iterator> <br> <a href="queryEmployee?curPageNumber=0">首页</a> <a href="queryEmployee?curPageNumber=${curPageNumber-1} ">上一页</a> <a href="queryEmployee?curPageNumber=${curPageNumber+1}">下一页</a> <a href="queryEmployee?curPageNumber=<s:property value="page.pageNums"/>">尾页</a> 一共<s:property value="page.pageNums"/>页 </form> </body> </html>
效果