pageBeen:
import java.util.List; /** * 分页封装 * */ public class PageBean<T> { private int currPage;//当前页数 private int pageSize;//每页显示记录数 private int totalCount;//总记录数 private int totalPage;//总页数 private List<T> list;//每页显示的信息 public int getCurrPage() { return currPage; } public void setCurrPage(int currPage) { this.currPage = currPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } }
Action类的分页方法:
// 分页(当前页)这里等于1是为了使第一页为默认页 private int currPage = 1; public int getCurrPage() { return currPage; } public void setCurrPage(int currPage) { this.currPage = currPage; } /** * 分页查询 */ public String findAll() { PageBean<Employee> pageBean = employeeService.findAll(currPage); ActionContext.getContext().getValueStack().push(pageBean); return "findAll"; }
Servlet中的分页方法:
public PageBean<Employee> findAll(int currPage) { PageBean<Employee> pageBean = new PageBean<>(); // 封装pageBean pageBean.setCurrPage(currPage); int pageSize = 3; pageBean.setPageSize(pageSize); int totalCount = employeeDao.findCount(); pageBean.setTotalCount(totalCount); Double totalPage = Math.ceil((double) totalCount / pageSize); pageBean.setTotalPage(totalPage.intValue()); int begin = (currPage - 1) * pageSize; List<Employee> list = employeeDao.findPage(begin, pageSize); pageBean.setList(list); return pageBean; }
在Dao层编写分页方法:
public int findCount() { String hql="select count(*) from Employee"; List<Long> list = (List<Long>) hibernateTemplate.find(hql); if(list.size()>0){ return list.get(0).intValue(); } return 0; } /** * 分页信息 */ public List<Employee> findPage(int begin, int pageSize) { DetachedCriteria criteria=DetachedCriteria.forClass(Employee.class); List<Employee> list = (List<Employee>) hibernateTemplate.findByCriteria(criteria, begin, pageSize); return list; }
视图层:
<table border="0" cellspacing="0" cellpadding="0" width="900px"> <tr> <td align="right"> <span>第<s:property value="currPage"/>/<s:property value="totalPage"/>页</span> <span>总记录数:<s:property value="totalCount"/>/每页显示:<s:property value="pageSize"/>条</span> <span> <s:if test="currPage!=1"> <a href="department_findAll.action?currPage=1">[首页]</a> <a href="department_findAll.action?currPage=<s:property value="currPage-1"/>">[上一页]</a> </s:if> <s:if test="currPage!=totalPage"> <a href="department_findAll.action?currPage=<s:property value="currPage+1"/>">[下一页]</a> <a href="department_findAll.action?currPage=<s:property value="totalPage"/>">[尾页]</a> </s:if> </span> </td> </tr> </table>
使用Struts2 进行迭代
<s:iterator value="list" var="d"> <tr> <td align="center"><s:property value="#d.dname"/></td> <td align="center"><a href="">编辑</a></td> <td align="center"><a href="">删除</a></td> </tr> </s:iterator>