我用的是SSH框架,但主要用到的分页逻辑应该是大同小异的;
首先定义工具类;
package com.util; import java.util.List; public class pageBean<E> { private List<E> list; //要返回的某一页的记录列表 private int allRow; //总记录数 private int totalPage; //总页数 private int currentPage; //当前页 private int pageSize; //每页的记录数 private boolean isFirstPage; //是否为当前第一页 private boolean isLastPage; //是否为最后一页 private boolean hasPreviousPage; //是否有前一页 private boolean hasNextPage; //是否有下一页 /** * 初始化分页信息 */ public void init(){ this.isFirstPage = isFirstPage; this.isLastPage = isLastPage; this.hasPreviousPage = hasPreviousPage; this.hasNextPage = hasNextPage; } /** * 计算总页数 静态方法 * @param pageSize 每页的记录数 * @param allRow 总记录数 * @return 总页数 */ public static int countTatalPage(final int pageSize,final int allRow){ int toalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize + 1; return toalPage; } /** * 计算当前页开始的记录 * @param pageSize 每页记录数 * @param currentPage 当前第几页 * @return 当前页开始记录号 */ public static int countOffset(final int pageSize,final int currentPage){ final int offset = pageSize * (currentPage - 1); return offset; } /** * 计算当前页,若为0或者请求的URL中没有“?page = ”则用1代替 * @param page 传入的参数(可能为空,即0 则返回1) * @return */ public static int countCurrentPage(int page){ final int curpage = (page == 0 ? 1 : page); return curpage; } public List<E> getList() { return list; } public void setList(List<E> list) { this.list = list; } public int getAllRow() { return allRow; } public void setAllRow(int allRow) { this.allRow = allRow; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public boolean isFirstPage() { return isFirstPage; } public void setFirstPage(boolean isFirstPage) { this.isFirstPage = isFirstPage; } public boolean isLastPage() { return isLastPage; } public void setLastPage(boolean isLastPage) { this.isLastPage = isLastPage; } public boolean isHasPreviousPage() { return hasPreviousPage; } public void setHasPreviousPage(boolean hasPreviousPage) { this.hasPreviousPage = hasPreviousPage; } public boolean isHasNextPage() { return hasNextPage; } public void setHasNextPage(boolean hasNextPage) { this.hasNextPage = hasNextPage; } }
然后要做的就是在action中查询一个结果集;并调用这个工具类,把结果集放进去;
public pageBean<TDoc> queryForPage(String hql,int pageSize, int page) { //final String hql = "from TZhaopin where del='no' order by fabushijian desc"; //查询语句 int allRow = memberDao.getAllRowCount(hql); //总记录数 int totalPage = pageBean.countTatalPage(pageSize, allRow); //总页数 final int offset = pageBean.countOffset(pageSize, page); //当前页开始记录 final int length = pageSize; // 每页记录数 final int currentPage = pageBean.countCurrentPage(page); // 当前页 List list = memberDao.queryForPage(hql, offset, length); // //把分页信息保存到Bean当中 pageBean<TDoc> pageBean = new pageBean<TDoc>(); pageBean.setPageSize(pageSize); pageBean.setCurrentPage(currentPage); pageBean.setAllRow(allRow); pageBean.setTotalPage(totalPage); pageBean.setList(list); pageBean.init(); return pageBean; }
然后就是把数据放到前台页面上就ok了;
<div id="box2" style="float: right;"> <s:iterator value="pageBean"> <tr> <td colspan="6" align="center" bgcolor="#5BA8DE"> <s:if test="%{currentPage == 1}"> 首 页 上一页 </s:if> <!-- currentPage为当前页 --> <s:else> <a href="index.action?page=1">首 页</a> <a href="index.action?page=<s:property value="%{currentPage-1}"/>">上一页</a> </s:else> <s:if test="%{currentPage != totalPage}"> <a href="index.action?page=<s:property value="%{currentPage+1}"/>">下一页</a> <a href="index.action?page=<s:property value="totalPage"/>">尾 页</a> </s:if> <s:else> 下一页 尾 页 </s:else> <br/> <!-- 共<s:property value="allRow"/>条记录 --> 共<s:property value="totalPage"/>页 当前第<s:property value="currentPage"/> 页 </td> </tr> </s:iterator> </div>
效果如下: