分頁實現的基本過程是這樣的:
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>