分页技术总结
2015-12-22 14:43
260 查看
分页在Web开发中是很常用的技术,下面介绍下最简单的分页实现:
首先介绍一下实现分页需要的类:
Page:分页信息类,封装了分页所需的所有信息,如每页显示记录数、总记录数、总页数、当前页、查询起始点、是否有上一页、是否有下一页。
PageUtil:分页辅助类,进行分页的时候,必须知道3个信息,分别是当前页、每页显示记录数、总记录数,其他信息都可以通过这三个信息计算得到。
Page类:
PageUtil类:
在Controller中封装好Page,传入Service获取相应的数据,然后将数据列表和Page对象返回前台,
页面写法:
到此一个简单的分页实现完毕。
首先介绍一下实现分页需要的类:
Page:分页信息类,封装了分页所需的所有信息,如每页显示记录数、总记录数、总页数、当前页、查询起始点、是否有上一页、是否有下一页。
PageUtil:分页辅助类,进行分页的时候,必须知道3个信息,分别是当前页、每页显示记录数、总记录数,其他信息都可以通过这三个信息计算得到。
Page类:
public class Page { private int everyPage; //每页显示记录数 private int totalCount; //总记录数 private int totalPage; //总页数 private int currentPage; //当前页 private int beginIndex; //查询起始点 private boolean hasPrePage; //是否有上一页 private boolean hasNextPage; //是否有下一页 public Page(int everyPage, int totalCount, int totalPage, int currentPage, int beginIndex, boolean hasPrePage, boolean hasNextPage) { super(); this.everyPage = everyPage; this.totalCount = totalCount; this.totalPage = totalPage; this.currentPage = currentPage; this.beginIndex = beginIndex; this.hasPrePage = hasPrePage; this.hasNextPage = hasNextPage; } public int getEveryPage() { return everyPage; } public void setEveryPage(int everyPage) { this.everyPage = everyPage; } 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 int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getBeginIndex() { return beginIndex; } public void setBeginIndex(int beginIndex) { this.beginIndex = beginIndex; } public boolean isHasPrePage() { return hasPrePage; } public void setHasPrePage(boolean hasPrePage) { this.hasPrePage = hasPrePage; } public boolean isHasNextPage() { return hasNextPage; } public void setHasNextPage(boolean hasNextPage) { this.hasNextPage = hasNextPage; } }
PageUtil类:
public class PageUtil { public static Page createPage (int everyPage, int totalCount, int currentPage) { everyPage = getEveryPage(everyPage); currentPage = getCurrentPage(currentPage); int totalPage = getTotalPage(everyPage, totalCount); int beginIndex = getBeginIndex(everyPage, currentPage); boolean hasPrePage = getHasPrePage(currentPage); boolean hasNextPage = getNextPage(totalPage, currentPage); return new Page(everyPage, totalCount, totalPage, currentPage, beginIndex, hasPrePage, hasNextPage); } public static int getEveryPage(int everyPage) { return everyPage == 0 ? 10 : everyPage; } public static int getCurrentPage(int currentPage) { return currentPage == 0 ? 1 : currentPage; } public static int getTotalPage(int everyPage, int totalCount) { int totalPage = 0; if (totalCount != 0 && totalCount%everyPage == 0) { totalPage = totalCount/everyPage; } else { totalPage = totalCount/everyPage + 1; } return totalPage; } public static int getBeginIndex (int everyPage, int currentPage) { return (currentPage - 1)*everyPage; } public static boolean getHasPrePage(int currentPage) { return currentPage != 1 ? true : false; } public static boolean getNextPage(int totalPage, int currentPage) { return totalPage == currentPage || totalPage == 0 ? false : true; } }
在Controller中封装好Page,传入Service获取相应的数据,然后将数据列表和Page对象返回前台,
页面写法:
<div> <c:choose> <c:when test="${page.hasPrePage}"> <a href="xxx?currentPage=1">首页</a> <a href="xxx?currentPage=${page.currentPage-1}">上一页</a> </c:when> <c:otherwise>首页|上一页</c:otherwise> </c:choose> <c:choose> <c:when test="${page.hasNextPage}"> <a href="xxx?currentPage=${page.currentPage+1}">下一页</a> <a href="xxx?currentPage=${page.totalPage}">尾页</a> </c:when> <c:otherwise>下一页|尾页</c:otherwise> </c:choose> 当前为第${page.currentPage}页,共${page.totalPage}页 </div>
到此一个简单的分页实现完毕。
相关文章推荐
- Objective-c语言_委托模式
- jquery的table每列得到ID,jquery组装table
- Android四大组件之Activity
- Linux操作系统启动过程详解
- windows下用Xshell远程登录Linux
- C#中Split用法~
- CentOS配置JDK环境
- 虚拟现实游戏的十大误区
- Renascence架构简介
- Apache对象池插件common-pool学习小结
- Android广播的发送与接收
- Android广播的发送与接收
- 自定义控件是把dp转化成px
- linux启动成功修改logo
- 用Wireshark简单分析HTTPS传输过程-抓包过程
- request.getParameter() request.getAttribute()区别
- Thinkphp编辑器扩展类kindeditor用法
- mysql 更新数据表的记录
- Arturs Sosins:一位程序员的互动访谈
- ifconfig找不到命令