pager.java
package com.jspnews.util; import java.io.Serializable; import java.util.List; /** * * <b> 分页通用类 </b> * * @author kangxu * @param <T> * */ public class Pager { /** * */ private static final long serialVersionUID = 4542617637761955078L; /** * currentPage 当前页 */ private int currentPage = 1; /** * pageSize 每页大小 */ private int pageSize = 10; /** * pageTotal 总页数 */ private int pageTotal; /** * recordTotal 总条数 */ private int recordTotal = 0; /** * previousPage 前一页 */ private int previousPage; /** * nextPage 下一页 */ private int nextPage; /** * firstPage 第一页 */ private int firstPage = 1; /** * lastPage 最后一页 */ private int lastPage; /** * 拼接url */ private String url = "?p="; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } // 以下set方式是需要赋值的 /** * 设置当前页 <br> * * @author kangxu * * @param currentPage */ public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } /** * 设置每页大小,也可以不用赋值,默认大小为10条 <br> * * @author kangxu * * @param pageSize */ public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * 设置总条数,默认为0 <br> * * @author kangxu * * @param recordTotal */ public void setRecordTotal(int recordTotal) { this.recordTotal = recordTotal; otherAttr(); } /** * 设置其他参数 * * @author kangxu * */ public void otherAttr() { // 总页数 this.pageTotal = this.recordTotal % this.pageSize > 0 ? this.recordTotal / this.pageSize + 1 : this.recordTotal / this.pageSize; // 第一页 this.firstPage = 1; // 最后一页 this.lastPage = this.pageTotal; // 前一页 if (this.currentPage > 1) { this.previousPage = this.currentPage - 1; } else { this.previousPage = this.firstPage; } // 下一页 if (this.currentPage < this.lastPage) { this.nextPage = this.currentPage + 1; } else { this.nextPage = this.lastPage; } } public int getCurrentPage() { return currentPage; } public int getPageSize() { return pageSize; } public int getPageTotal() { return pageTotal; } public int getRecordTotal() { return recordTotal; } public int getPreviousPage() { return previousPage; } public int getNextPage() { return nextPage; } public int getFirstPage() { return firstPage; } public int getLastPage() { return lastPage; } @Override public String toString() { return "Pager [currentPage=" + currentPage + ", pageSize=" + pageSize + ", pageTotal=" + pageTotal + ", recordTotal=" + recordTotal + ", previousPage=" + previousPage + ", nextPage=" + nextPage + ", firstPage=" + firstPage + ", lastPage=" + lastPage + ", url=" + url + "]"; } }
test
package com.jspnews.test; import com.jspnews.util.Pager; public class TestPager { public static void main(String[] args) { Pager pager = new Pager(); pager.setCurrentPage(-1); pager.setPageSize(4); pager.setRecordTotal(21); System.out.println(pager.getPreviousPage() + "," + pager.getNextPage()); pager.setCurrentPage(0); pager.setPageSize(4); pager.setRecordTotal(21); System.out.println(pager.getPreviousPage() + "," + pager.getNextPage()); pager.setCurrentPage(1); pager.setPageSize(4); pager.setRecordTotal(15); System.out.println(pager.getPreviousPage() + "," + pager.getNextPage()); pager.setCurrentPage(2); pager.setPageSize(4); pager.setRecordTotal(21); System.out.println(pager.getPreviousPage() + "," + pager.getNextPage()); pager.setCurrentPage(3); pager.setPageSize(4); pager.setRecordTotal(21); System.out.println(pager.getPreviousPage() + "," + pager.getNextPage()); pager.setCurrentPage(4); pager.setPageSize(4); pager.setRecordTotal(21); System.out.println(pager.getPreviousPage() + "," + pager.getNextPage()); pager.setCurrentPage(5); pager.setPageSize(4); pager.setRecordTotal(21); System.out.println(pager.getPreviousPage() + "," + pager.getNextPage()); pager.setCurrentPage(6); pager.setPageSize(4); pager.setRecordTotal(21); System.out.println(pager.getPreviousPage() + "," + pager.getNextPage()); pager.setCurrentPage(7); pager.setPageSize(4); pager.setRecordTotal(21); System.out.println(pager.getPreviousPage() + "," + pager.getNextPage()); } }
result
1,0 1,1 1,2 1,3 2,4 3,5 4,6 5,6 6,6