1 public class PageUtil { 2 private int totalCount;//總數 3 private int pageSize=10;//每頁顯示數量 4 private int currpageNum;//當前頁 5 private int pageCount;//總頁數 6 private int prePage;//上一頁 7 private int nextPage;//下一頁 8 private boolean hasPrePage;//是否有上一頁 9 private boolean hasNextPage;//是否有下一頁 10 private int firstPage;//第一頁 11 private int lastPage;//最后一頁 12 private int currentcount;//當前從第多少條數據開始顯示 13 14 public PageUtil() { 15 16 } 17 public PageUtil(int totalCount,int pageNum){ 18 this.totalCount =totalCount; 19 this.currpageNum=pageNum; 20 this.pageCount = (int) Math.ceil(1.0*totalCount/pageSize); 21 this.currentcount =(pageCount-1)*pageSize; 22 if(pageNum>1){ //判斷是不是第一頁 23 /*--不是第一頁 則有上一頁 ,也有第一頁--*/ 24 hasPrePage=true; 25 prePage = pageNum-1; 26 firstPage =1; 27 } 28 if(pageNum<pageCount){//判斷是不是最后一頁 29 /*--不是最后一頁 則有上一頁 ,也有最后一頁--*/ 30 hasNextPage=true; 31 nextPage=pageNum+1; 32 lastPage=pageCount; 33 } 34 } 35 public int getTotalCount() { 36 return totalCount; 37 } 38 public void setTotalCount(int totalCount) { 39 this.pageCount = (int) Math.ceil(1.0*totalCount/pageSize); 40 if(this.currpageNum < 1) 41 { 42 this.currpageNum = 1 ; 43 } 44 this.currentcount =(currpageNum-1)*pageSize; 45 if(currpageNum>1){ //判斷是不是第一頁 46 /*--不是第一頁 則有上一頁 ,也有第一頁--*/ 47 hasPrePage=true; 48 prePage = currpageNum-1; 49 firstPage =1; 50 } 51 if(currpageNum<pageCount){//判斷是不是最后一頁 52 /*--不是最后一頁 則有上一頁 ,也有最后一頁--*/ 53 hasNextPage=true; 54 nextPage=currpageNum+1; 55 lastPage=pageCount; 56 } 57 this.totalCount = totalCount; 58 } 59 public int getPageSize() { 60 return pageSize; 61 } 62 public void setPageSize(int pageSize) { 63 this.pageSize = pageSize; 64 } 65 public int getPrePage() { 66 return prePage; 67 } 68 public void setPrePage(int prePage) { 69 this.prePage = prePage; 70 } 71 public int getNextPage() { 72 return nextPage; 73 } 74 public void setNextPage(int nextPage) { 75 this.nextPage = nextPage; 76 } 77 public boolean isHasPrePage() { 78 return hasPrePage; 79 } 80 public void setHasPrePage(boolean hasPrePage) { 81 this.hasPrePage = hasPrePage; 82 } 83 public boolean isHasNextPage() { 84 return hasNextPage; 85 } 86 public void setHasNextPage(boolean hasNextPage) { 87 this.hasNextPage = hasNextPage; 88 } 89 public int getFirstPage() { 90 return firstPage; 91 } 92 public void setFirstPage(int firstPage) { 93 this.firstPage = firstPage; 94 } 95 public int getLastPage() { 96 return lastPage; 97 } 98 public void setLastPage(int lastPage) { 99 this.lastPage = lastPage; 100 } 101 102 public int getCurrpageNum() { 103 return currpageNum; 104 } 105 public void setCurrpageNum(int currpageNum) { 106 this.currpageNum = currpageNum; 107 } 108 public int getPageCount() { 109 return pageCount; 110 } 111 public void setPageCount(int pageCount) { 112 this.pageCount = pageCount; 113 } 114 public int getCurrentcount() { 115 return currentcount; 116 } 117 public void setCurrentcount(int currentcount) { 118 this.currentcount = currentcount; 119 } 120 121 122 }
說明:
構造函數: 一個是無參的 一個是有參的; 不論使用哪個都需要先創建(new)對象, 當然也可以設置靜態類;
無參構造函數:
使用無參構造函數 ,只需要從數據庫中查詢到 數據的總條數 count(*),調用pageutil.setTotalCount("總的數據條數"), 就可以進行分頁了; 默認每頁10條數據
有參構造函數:
使用有參構造函數 ,只需要從數據庫中查詢到 數據的總條數 count(*),並且傳入要每頁分多少條數據,初始化對象, 就可以進行分頁了;
一般通過request.attribute("pageUtil",pageUtil),就可以在JSP頁面進行分頁