Java paging code

Source: Internet
Author: User

Java paging code

First, the main class is pageview, which encapsulates all the information required to construct a page. As follows:

Public class pageview <t> {

/** Paging data **/

Private list <t> records;

/** Start Index and end index on the page number **/

Private pageindex;

/** Total Page number **/

Private long totalpage = 1;

/** Number of records displayed per page **/

Private int maxresult = 12;

/** Current page **/

Private int currentpage = 1;

/** Total number of records **/

Private long totalrecord;

/** Page number **/

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 indicates that the page number starts and ends the index.

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;

    }

   

}

The getpageindex method is used to determine the pageindex status when currenpage is reached, as follows:

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 );

  }

In addition, place the total number of records in the result set in queryrst.

Public class queryrst <t> {

Private list <t> rstlist;

Private long totalrecord;

// Getter & setter...

}

In use,

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

Pageview. setqueryresult (qr );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.