import java.util.List; public class PageView { // 通過參數(shù)指定的信息 private int currentPage;// 當(dāng)前頁碼 private int pageSzie;// 每頁顯示記錄數(shù)量 // 通過查詢數(shù)據(jù)庫獲取的信息,外部獲取 private int recordTotal;// 總記錄數(shù) private List recordList;// 當(dāng)前面記錄信息列表 // 通過計(jì)算生成的信息 private int pageTotal;// 總頁面數(shù)量 private int startIndex;// 起始頁面索引 private int endIndex;// 結(jié)束頁面索引 // 顯示的頁面數(shù)量 private static final int PAGE_INDEX_COUNT = 10; // 在構(gòu)造方法中生成各種需要的信息 public PageView(int currentPage, int pageSize, int recordTotal, List recordList) { this.currentPage = currentPage; this.pageSzie = pageSize; this.recordTotal = recordTotal; this.recordList = recordList; // 通過計(jì)算生成startIndex和endIndex /* * 因?yàn)轱@示的頁面索引數(shù)量是有限的 我們不能把所以的頁面索引一下子全列出來 我們需要?jiǎng)討B(tài)顯示頁面索引列表 */ this.pageTotal = (this.recordTotal + this.pageSzie - 1) / this.pageSzie; // 如果頁面總數(shù)<=顯示頁面索引數(shù)量 if (this.pageTotal <= PAGE_INDEX_COUNT) { this.startIndex = 1; this.endIndex = this.pageTotal; } else { // 根據(jù)當(dāng)前頁面索引生成,頁面起始索引和結(jié)束索引。 // 區(qū)分偶數(shù)和奇數(shù) 頁面索引數(shù)量 if (PAGE_INDEX_COUNT % 2 == 0) { this.startIndex = this.currentPage - (PAGE_INDEX_COUNT / 2 - 1); this.endIndex = this.currentPage + (PAGE_INDEX_COUNT / 2); } else { this.startIndex = this.currentPage - (PAGE_INDEX_COUNT / 2); this.endIndex = this.currentPage + (PAGE_INDEX_COUNT / 2); } // 如果生成的起始索引小于1 if(this.startIndex < 1){ this.startIndex = 1; this.endIndex = PAGE_INDEX_COUNT; } // 如果生成的結(jié)束索引大于總頁面索引數(shù)量 if(this.endIndex > this.pageTotal){ this.endIndex = this.pageTotal; this.startIndex = this.pageTotal - PAGE_INDEX_COUNT; } } } // ...getters AND setters } |