1. import java.util.List;  

  2.   

  3. /** 

  4.  * 分页类 

  5.  * @author hxl 

  6.  * 

  7.  * @param <T> 

  8.  */  

  9.   

  10. public class Page<T> {  

  11.   

  12.     private final static int PAGEITEMCOUNT = 10;  //显示页码条目数,即页码数量顶多是10个  

  13.       

  14.     private List<T> list;         //保存查询的结果集合  

  15.     private int totalRecord;        //总记录数  

  16.     private int pageSize = 5;       //页面显示的数目  

  17.     private Integer totalPage;          //总页码数  

  18.     private int currentPage = 1;    //当前页码  

  19.     private int previousPage;       //前一页  

  20.     private int nextPage;           //后一页  

  21.     private int[] pageBar;          //条目数  

  22.     private int startIndex;         //开始页  

  23.     private int endIndex;           //结束页  

  24.       

  25. /*  public int getSelectEndIndex() {     //oracle中的结束查询条件,供传入数据库参数使用 

  26.         return this.getStartIndex()+this.pageSize - 1;   

  27.     }*/  

  28.       

  29.     public int getStartIndex() {  

  30.         this.startIndex = (this.currentPage-1)*this.pageSize;     

  31.         return startIndex;  

  32.     }  

  33.     public void setStartIndex(int startIndex) {  

  34.         this.startIndex = startIndex;  

  35.     }  

  36.     public int getEndIndex() {   //从数据库中获取的结束索引,供页面使用  

  37.         int end = this.getStartIndex() + this.getPageSize();  //不包含最后一条记录-1  

  38.         if(end>this.getTotalRecord()){  

  39.             end = this.getStartIndex() + (this.getTotalRecord()%this.getPageSize());  

  40.         }  

  41.         this.endIndex = end;  

  42.         return this.endIndex;  

  43.     }  

  44.     public void setEndIndex(int endIndex) {  

  45.         this.endIndex = endIndex;  

  46.     }  

  47.     public List<T> getList() {  

  48.         return list;  

  49.     }  

  50.     public void setList(List<T> list) {  

  51.         this.list = list;  

  52.     }  

  53.     public int getTotalRecord() {  

  54.         return totalRecord;  

  55.     }  

  56.     public void setTotalRecord(int totalRecord) {  

  57.         this.totalRecord = totalRecord;  

  58.     }  

  59.     public int getPageSize() {  

  60.         return pageSize;  

  61.     }  

  62.     public void setPageSize(int pageSize) {  

  63.         this.pageSize = pageSize;  

  64.     }  

  65.     public Integer getTotalPage() {             //得到总页码数  

  66.         if(this.totalRecord%this.pageSize==0){  

  67.             this.totalPage = this.totalRecord/this.pageSize;  

  68.         }else{  

  69.             this.totalPage = this.totalRecord/this.pageSize+1;  

  70.         }  

  71.           

  72.         return totalPage;  

  73.     }  

  74.       

  75.     public int getCurrentPage() {  

  76.         return currentPage;  

  77.     }  

  78.     public void setCurrentPage(int currentPage) {  

  79.         this.currentPage = currentPage;  

  80.     }  

  81.     public int getPreviousPage() {  

  82.         if(this.currentPage-1<1){      //如果上一页小于1,则说明当前页码已经是第一页了  

  83.             this.previousPage = 1;  

  84.         }else{  

  85.             this.previousPage = this.currentPage-1;  

  86.         }  

  87.         return previousPage;  

  88.     }  

  89.       

  90.     public int getNextPage() {             

  91.         if(this.currentPage+1>=this.totalPage){   //如果下一页大于总数页,则说明当前页码已经是最后一页了  

  92.             this.nextPage = this.totalPage;  

  93.         }else{  

  94.             this.nextPage = this.currentPage +1;  

  95.         }  

  96.         return nextPage;  

  97.     }  

  98.       

  99.     public int[] getPageBar() {  

  100.         int startPage;      //记住页码的起始位置  

  101.         int endPage;        //记住页码的结束位置,因为页码条目是既定的,由startpage,endpage两个变量通过循环就可以得全部页码  

  102.         int pageBar[] = null;  

  103.         if(this.getTotalPage() <= PAGEITEMCOUNT){    //当总页码不足或等于既定页面大小时  

  104.             pageBar = new int[this.totalPage];  

  105.             startPage = 1;  

  106.             endPage = this.totalPage;  

  107.         }else{                  //当总页码大于既定页面大小时  

  108.             pageBar = new int[PAGEITEMCOUNT];  

  109.             startPage = this.currentPage - (PAGEITEMCOUNT/2-1);    //为了保证当前页在中间  

  110.             endPage = this.currentPage + PAGEITEMCOUNT/2;  

  111.   

  112.             if(startPage<1){  

  113.                 startPage = 1;  

  114.                 endPage = PAGEITEMCOUNT;  

  115.             }  

  116.               

  117.             if(endPage>this.totalPage){  

  118.                 endPage = this.totalPage;  

  119.                 startPage = this.totalPage - (PAGEITEMCOUNT-1);  

  120.             }  

  121.         }  

  122.           

  123.         int index = 0;  

  124.         for(int i=startPage;i<=endPage;i++){  

  125.             pageBar[index++] = i;  

  126.         }  

  127.           

  128.         this.pageBar = pageBar;  

  129.         return this.pageBar;  

  130.     }  

  131.       

  132. }