Java (STRUTS2-based) paging implementation code _java

Source: Internet
Author: User

The basic process of paging implementation is this:

1. Set the basic parameters of your own paging (you can read from the configuration file)

The number of record bars displayed per page

How many pages are displayed at a time

2. Write a function that sets the other parameters of the pager

The main parameters are as follows:

Total Record Bar number

Total pages

Current page Number: pages now displayed

The number of record bars displayed per page

Current page start line (first line is 0 lines)

First page number

Last page number

Next page number

Previous page number

The starting page number displayed on the screen

The ending page number displayed on the screen

The basic principle of the implementation of parameters: Set the above parameters, in fact, only three parameters can be set to all other variables, that is, the total number of records, each page shows the number of records, each display how many pages.

The code for the pager is implemented as follows (omitting the Get,set function):

Page.java

Copy Code code as follows:



{


This.onepagesize = integer.valueof (Pageresource.get (pageresource.one_page_size));


This.displaypagecount = integer.valueof (Pageresource.get (Pageresource.display_page_count))-1;


}





/** page number navigation, the maximum number of Display page number for displaypagecount+1 * *


private int displaypagecount;

/** the number of record bars displayed per page * *
private int onepagesize;

/** Total record number of articles * *
private int Totalrecord;

/** Total Pages * *
private int totalpage;

/** Current Page Number * *
private int currentpagenum = 1;

/** Current page start line (first line is 0 lines) * *
private int currentstartrow;

/** first page Number * *
private int firstpagenum = 1;

/** last page Number * *
private int lastpagenum;

/** Next Page Number * *
private int nextpagenum;

/** last page Number * *
private int prevpagenum;

/** page Number navigation start Page number * *
private int startpagenum;

/** page Number type navigation End page Number * *
private int endpagenum;

/**
*
* @param onepagesize
* @param currentpagenum
* @param Totalrecord
*/
Public Page (int totalrecord) {
This.totalrecord = Totalrecord;
This.setpageinfo ();
}

Public Page () {
}

public void Setpageinfo () {
This.totalpage = (Totalrecord + onePageSize-1)/onepagesize;
This.currentpagenum = Math.max (1, Math.min (Currentpagenum, totalpage));

        this.lastpagenum = this.totalpage;
        this.nextpagenum = Math.min (this.totalpage, This.currentpagenum + 1);
        this.prevpagenum = Math.max (1, this.currentpagenum-1);

Paging Control information
This.currentstartrow = (this.currentpagenum-1) * onepagesize;

Startpagenum = Math.max (THIS.CURRENTPAGENUM-DISPLAYPAGECOUNT/2,
Firstpagenum);
Endpagenum = math.min (Startpagenum + displaypagecount, lastpagenum);
if (Endpagenum-startpagenum < Displaypagecount) {
Startpagenum = Math.max (Endpagenum-displaypagecount, 1);
}
}




3. Write front-end code (take Struts2 as an example)

When you click on each jump page in the foreground link, you only need to jump to the page number and total pages to the background, the background will update the pager, and then achieve page number jump.

Copy Code code as follows:



&lt;div&gt;


&lt;div&gt;


Total Pages:


&lt;s:property value= "#request. P.totalpage"/&gt;


Total record number:


&lt;s:property value= "#request. P.totalrecord"/&gt;


&lt;/div&gt;


&lt;s:url id= "Firsturl" action= "Pageaction!topage" &gt;


&lt;s:param name= "P.currentpagenum" &gt;


&lt;s:property value= "#request. P.firstpagenum"/&gt;


&lt;/s:param&gt;


&lt;s:param name= "P.totalrecord" &gt;


&lt;s:property value= "#request. P.totalrecord"/&gt;


&lt;/s:param&gt;


&lt;/s:url&gt;


&lt;s:a href= "%{firsturl}" &gt; Home &lt;/s:a&gt;





&lt;s:url id= "prev" action= "Pageaction!topage" &gt;


&lt;s:param name= "P.currentpagenum" &gt;


&lt;s:property value= "#request. P.prevpagenum"/&gt;


&lt;/s:param&gt;


&lt;s:param name= "P.totalrecord" &gt;


&lt;s:property value= "#request. P.totalrecord"/&gt;


&lt;/s:param&gt;


&lt;/s:url&gt;


&lt;s:a href= "%{prev}" &gt; Prev &lt;/s:a&gt;

&lt;s:bean name= "Org.apache.struts2.util.Counter" id= "Counter" &gt;


&lt;s:param name= "The" "Value=" P.startpagenum "/&gt;


&lt;s:param name= "Last" value= "P.endpagenum"/&gt;


&lt;s:iterator var= "Pagenum" &gt;


&lt;s:if test= "p.currentpagenum== #pageNum" &gt;


&lt;s:property/&gt;


&lt;/s:if&gt;


&lt;s:else&gt;


&lt;s:url id= "page" action= "Pageaction!topage" &gt;


&lt;s:param name= "P.currentpagenum" &gt;


&lt;s:property value= "#pageNum"/&gt;


&lt;/s:param&gt;


&lt;s:param name= "P.totalrecord" &gt;


&lt;s:property value= "#request. P.totalrecord"/&gt;


&lt;/s:param&gt;


&lt;/s:url&gt;


&lt;s:a href= "%{page}" &gt;&lt;s:property/&gt;&lt;/s:a&gt;


&lt;/s:else&gt;


&lt;/s:iterator&gt;


&lt;/s:bean&gt;

&lt;s:url id= "Next" action= "Pageaction!topage" &gt;


&lt;s:param name= "P.currentpagenum" &gt;


&lt;s:property value= "#request. P.nextpagenum"/&gt;


&lt;/s:param&gt;


&lt;s:param name= "P.totalrecord" &gt;


&lt;s:property value= "#request. P.totalrecord"/&gt;


&lt;/s:param&gt;


&lt;/s:url&gt;


&lt;s:a href= "%{next}" &gt; next page &lt;/s:a&gt;





&lt;s:url id= "Lasturl" action= "Pageaction!topage" &gt;


&lt;s:param name= "P.currentpagenum" &gt;


&lt;s:property value= "#request. P.lastpagenum"/&gt;


&lt;/s:param&gt;


&lt;s:param name= "P.totalrecord" &gt;


&lt;s:property value= "#request. P.totalrecord"/&gt;


&lt;/s:param&gt;


&lt;/s:url&gt;


&lt;s:a href= "%{lasturl}" &gt; last page &lt;/s:a&gt;


&lt;/div&gt;


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.