Java 分页实现(基于Struts2)
分页实现的基本过程是这样的:
1. 设置自己的分页器的基本参数(可以从配置文件中读取)
■每页显示的记录条数
■每次最多显示多少页
2. 编写设置分页器其他参数的函数
主要参数有以下几个:
总记录条数
总页数
当前页号:现在显示的页数
每页显示的记录条数
当前页开始行(第一行是0行)
第一页页号
最后页页号
下一页页号
上一页页号
画面上显示的起始页号
画面上显示的结束页号
参数基本实现原理:设置以上各个参数,实际上只需要三个参数就可以对所有的其他变量进行设置,即总记录条数,每页显示记录数,每次最多显示多少页。
分页器的代码实现如下(省略get,set函数):
Page.java
{ this.onePageSize = Integer.valueOf(PageResource.get(PageResource.ONE_PAGE_SIZE)); this.displayPageCount = Integer.valueOf(PageResource.get(PageResource.DISPLAY_PAGE_COUNT)) - 1; } /** 页号式导航, 最多显示页号数量为displayPageCount+1 */ private int displayPageCount; /** 每页显示的记录条数 */ private int onePageSize; /** 总记录条数 */ private int totalRecord; /** 总页数 */ private int totalPage; /** 当前页号 */ private int currentPageNum = 1; /** 当前页开始行(第一行是0行) */ private int currentStartRow; /** 第一页页号 */ private int firstPageNum = 1; /** 最后页页号 */ private int lastPageNum; /** 下一页页号 */ private int nextPageNum; /** 上一页页号 */ private int prevPageNum; /** 页号式导航 起始页号 */ private int startPageNum; /** 页号式导航 结束页号 */ private int endPageNum; /** * * @param onePageSize * @param currentPageNum * @param totalRecord */ public Page(int totalRecord) { this.totalRecord = totalRecord; this.setPageInfo(); } public Page() { } public void setPageInfo() { this.totalPage = (totalRecord + onePageSize - 1) / onePageSize; this.currentPageNum = Math.max(1, Math.min(currentPageNum, totalPage)); this.lastPageNum = this.totalPage; this.nextPageNum = Math.min(this.totalPage, this.currentPageNum + 1); this.prevPageNum = Math.max(1, this.currentPageNum - 1); // 分页控制信息 this.currentStartRow = (this.currentPageNum - 1) * onePageSize; startPageNum = Math.max(this.currentPageNum - displayPageCount / 2, firstPageNum); endPageNum = Math.min(startPageNum + displayPageCount, lastPageNum); if (endPageNum - startPageNum < displayPageCount) { startPageNum = Math.max(endPageNum - displayPageCount, 1); } }
3. 编写前端代码(以Struts2为例)
当在前台点击各个跳转页面的链接时,只需要将要跳转到的页号和总页数传给后台,后台会重新更新分页器,进而实现页码的跳转。
<div> <div> 总页数: <s:property value="#request.p.totalPage" /> 总记录数: <s:property value="#request.p.totalRecord" /> </div> <s:url id="firstURL" action="PageAction!toPage"> <s:param name="p.currentPageNum"> <s:property value="#request.p.firstPageNum" /> </s:param> <s:param name="p.totalRecord"> <s:property value="#request.p.totalRecord" /> </s:param> </s:url> <s:a href="%{firstURL}">首页</s:a> <s:url id="prev" action="PageAction!toPage"> <s:param name="p.currentPageNum"> <s:property value="#request.p.prevPageNum" /> </s:param> <s:param name="p.totalRecord"> <s:property value="#request.p.totalRecord" /> </s:param> </s:url> <s:a href="%{prev}">上一页</s:a>
<s:bean name="org.apache.struts2.util.Counter" id="counter"> <s:param name="first" value="p.startPageNum" /> <s:param name="last" value="p.endPageNum" /> <s:iterator var="pageNum"> <s:if test="p.currentPageNum==#pageNum"> <s:property /> </s:if> <s:else> <s:url id="page" action="PageAction!toPage"> <s:param name="p.currentPageNum"> <s:property value="#pageNum" /> </s:param> <s:param name="p.totalRecord"> <s:property value="#request.p.totalRecord" /> </s:param> </s:url> <s:a href="%{page}"><s:property /></s:a> </s:else> </s:iterator> </s:bean>
<s:url id="next" action="PageAction!toPage"> <s:param name="p.currentPageNum"> <s:property value="#request.p.nextPageNum" /> </s:param> <s:param name="p.totalRecord"> <s:property value="#request.p.totalRecord" /> </s:param> </s:url> <s:a href="%{next}">下一页</s:a> <s:url id="lastURL" action="PageAction!toPage"> <s:param name="p.currentPageNum"> <s:property value="#request.p.lastPageNum" /> </s:param> <s:param name="p.totalRecord"> <s:property value="#request.p.totalRecord" /> </s:param> </s:url> <s:a href="%{lastURL}">尾页</s:a> </div>