PageUtil ,简单的分页工具

  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页面进行分页

原文地址:https://www.cnblogs.com/bignew/p/6594734.html