Java imitation Baidu Paging

Source: Internet
Author: User

Recently, due to business needs, I wrote a paging tool similar to Baidu, which I felt a little bit and posted for your reference and correction.

In fact, the paging tool class is not difficult to write. You can easily write pages by figuring out the following items:

1. How to obtain the current page (nowpage)

2. How to obtain the total number of pages (totalpage)

3. How to calculate the start page

4. How to calculate the end page

5. What are the query conditions? How to assign query conditions to corresponding classes through pages

6. How can I query the object Array Based on the query conditions? This array (usually used as a set list) is used to print the page cyclically.

7. Paging mechanism. The code I posted is similar to Baidu's paging mechanism. The page displays 10 page numbers and 5 data entries per page. As for this mechanism, you can easily understand it after studying it. The general meaning is that the current page is always in the middle and the displayed page number is 10.

8. Confidence-believe that you can write good pages!

Import java. util. List;

Import com. cmnet. www. Bean. page;

/**
* Paging Tool
*
* @ Author Jasper
*
*/
Public class pagination {
Private int nowpage; // current page
Private int startpage; // The start page.
Private int endpage; // end page
Private int totalrecord; // The total number of records.
Private int totalpage; // the total number of pages.
Private List objects; // set of objects identified by conditions
Private conditions; // query Conditions
Public static final int pagesize = 5; // number of entries per page
Public static final int showpages = 10; // Number of pages displayed on each page

Public pagination (){

}

/**
* Paging Method
* In this method, two data types are obtained: startpage and endpage.
* The page number is displayed after the two data are processed.
* @ Param nowpage current page
* @ Param totalpage: Total number of pages
*/
Public void paginationtool (INT nowpage, int totalpage ){
This. nowpage = nowpage;
This. totalpage = totalpage;
/**
* Calculate the value of startpage and endpage.
*
*/
If (this. totalpage <showpages ){
/** If the total page number is smaller than showpages */
This. startpage = 1;
This. endpage = totalpage;
} Else {
/** In else, the total number of pages is greater than showpages */
If (this. nowpage <= showpages/2 + 1 ){
This. startpage = 1;
This. endpage = showpages;
} Else {
This. startpage = This. nowpage-(showpages/2 );
This. endpage = This. nowpage + (showpages/2-1 );
If (this. endpage> = This. totalpage ){
This. endpage = This. totalpage;
This. startpage = This. totalpage-showpages + 1;
}
}
}
}

Public conditions getconditions (){
Return conditions;
}

Public void setconditions (conditions ){
This. Conditions = conditions;
}

Public int getnowpage (){
Return nowpage;
}

Public void setnowpage (INT nowpage ){
This. nowpage = nowpage;
}

Public int gettotalrecord (){
Return totalrecord;
}

Public void settotalrecord (INT totalrecord ){
This. totalrecord = totalrecord;
}

Public int gettotalpage (){
Return totalpage;
}

Public void settotalpage (INT totalpage ){
This. totalpage = totalpage;
}

Public list getobjects (){
Return objects;
}

Public void setobjects (List objects ){
This. Objects = objects;
}

Public static int getshowpages (){
Return showpages;
}

Public int getstartpage (){
Return startpage;
}

Public void setstartpage (INT startpage ){
This. startpage = startpage;
}

Public int getendpage (){
Return endpage;
}

Public void setendpage (INT endpage ){
This. endpage = endpage;
}

}

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.