java分頁代碼用於jsp及java並帶有測試方法

import java.util.ArrayList;
import java.util.List;

public class FenYe
{
     static double allpage=0; //總頁數
     static double currpage=0; //當前頁
     static double pagesize=10; //顯示行
     static int startindex=0; //起始索引
     static int endindex=0; //結束索引
     static List alldata=null; //分完的數據

    /**
     * 得到當前頁
     * @return
     */
    public static  int getCurrpage() {
  return (int)currpage;
 }

    /**
     * 設置當前頁
     * @param currpage
     */
 public void setCurrpage(int currpage) {
  currpage = currpage;
 }

 /**
  * 得到頁大小(每頁顯示的行數)
  * @return
  */
 public static int getPagesize() {
  return (int)pagesize;
 }

 /**
  * 設置頁大小(每頁顯示的行數)
  * @param pagesize
  */
 public  void setPagesize(int pagesize) {
  pagesize = pagesize;
 }

 public static List getFenYe(List indata,int curpage)
    {  
     if(curpage<=1){
      currpage=1;
     }else{
      currpage=curpage;      
     }
      allpage=(int)(Math.ceil(((double)indata.size())/pagesize));
     if(curpage>allpage){
      currpage=allpage;
     }
     startindex=(int) ((currpage-1)*pagesize);
     endindex=(int) ((currpage-1)*pagesize+pagesize);      
     if(endindex>=indata.size())
     {
      endindex=indata.size();
     }
     for(int i=startindex;i<endindex;i++){
      if(alldata==null){
       alldata=new ArrayList();
      }      
      alldata.add(indata.get(i));
     }
     
  return alldata;
    }
   
    public static void main(String[] args) {
  ArrayList ls=new ArrayList();
  for(int i=0;i<13;i++){
   ls.add(i);
  }
  
  List le=FenYe.getFenYe(ls,6);
  
  for(int i=0;i<le.size();i++){
   System.out.println(le.get(i)+"||");   
  }
  System.out.println("當前頁"+FenYe.getCurrpage()+"|每頁行數"+FenYe.getPagesize());
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章