java分頁類代碼

來源:互聯網
上載者:User

java分頁類代碼

首先,最主要的一個類是 pageview ,其中封裝了構造一個頁面所需全部資訊。如下:

public class pageview<t> {

    /** 分頁資料 **/

    private list<t> records;

    /** 頁碼開始索引和結束索引 **/

    private pageindex pageindex;

    /** 總頁數 **/

    private long totalpage = 1;

    /** 每頁顯示記錄數 **/

    private int maxresult = 12;

    /** 當前頁 **/

    private int currentpage = 1;

    /** 總記錄數 **/

    private long totalrecord;

    /** 頁碼數量 **/

    private int pagecode = 10;

   

    public pageview(int maxresult, int currentpage) {

       this.maxresult = maxresult;

       this.currentpage = currentpage;

    }

   

    public void setqueryresult(queryrst qr){

       settotalrecord(qr.gettotalrecord());

       setrecords(qr.getrstlist());

    }

   

    public int getpagecode() {

       return pagecode;

    }

 

    public void setpagecode(int pagecode) {

       this.pagecode = pagecode;

    }

 

    public long gettotalrecord() {

       return totalrecord;

    }

    public void settotalrecord(long totalrecord) {

       this.totalrecord = totalrecord;

       settotalpage(this.totalrecord%this.maxresult==0? this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);

    }

    public list<t> getrecords() {

       return records;

    }

    public void setrecords(list<t> records) {

       this.records = records;

    }

    public pageindex getpageindex() {

       return pageindex;

    }

    public long gettotalpage() {

       return totalpage;

    }

    public void settotalpage(long totalpage) {

       this.totalpage = totalpage;

       this.pageindex = webtool.getpageindex(pagecode, currentpage, totalpage);

    }

    public int getmaxresult() {

       return maxresult;

    }

    public int getcurrentpage() {

       return currentpage;

    }

}

其中pageindex,說明頁碼開始索引和結束索引

public class pageindex {

    private long startindex;

    private long endindex;

   

    public pageindex(long startindex, long endindex) {

       this.startindex = startindex;

       this.endindex = endindex;

    }

    public long getstartindex() {

       return startindex;

    }

    public void setstartindex(long startindex) {

       this.startindex = startindex;

    }

    public long getendindex() {

       return endindex;

    }

    public void setendindex(long endindex) {

       this.endindex = endindex;

    }

   

}

涉及到的getpageindex方法為,用於確定當到達currenpage時的pageindex狀況,如下:

  public static pageindex getpageindex(long viewpagecount, int currenpage, long totalpage){

       long startpage = currenpage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);

       long endpage = currenpage+viewpagecount/2;

       if(startpage<1){

           startpage = 1;

           if(totalpage>=viewpagecount) endpage = viewpagecount;

           else endpage = totalpage;

       }

        if(endpage>totalpage){

           endpage = totalpage;

           if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;

           else startpage = 1;

       }

       return new pageindex(startpage, endpage);    

  }

另外,將結果集總記錄數放在queryrst裡面

public class queryrst<t> {

    private list<t> rstlist;

    private long totalrecord;

    // getter & setter ...

}

使用時,

pageview<?> pageview = new pageview<?>(12,currentpage);

pageview.setqueryresult(qr);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.