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:
<div>
<div>
Total Pages:
<s:property value= "#request. P.totalpage"/>
Total record number:
<s:property value= "#request. P.totalrecord"/>
</div>
<s:url id= "Firsturl" action= "Pageaction!topage" >
<s:param name= "P.currentpagenum" >
<s:property value= "#request. P.firstpagenum"/>
</s:param>
<s:param name= "P.totalrecord" >
<s:property value= "#request. P.totalrecord"/>
</s:param>
</s:url>
<s:a href= "%{firsturl}" > Home </s:a>
<s:url id= "prev" action= "Pageaction!topage" >
<s:param name= "P.currentpagenum" >
<s:property value= "#request. P.prevpagenum"/>
</s:param>
<s:param name= "P.totalrecord" >
<s:property value= "#request. P.totalrecord"/>
</s:param>
</s:url>
<s:a href= "%{prev}" > Prev </s:a>
<s:bean name= "Org.apache.struts2.util.Counter" id= "Counter" >
<s:param name= "The" "Value=" P.startpagenum "/>
<s:param name= "Last" value= "P.endpagenum"/>
<s:iterator var= "Pagenum" >
<s:if test= "p.currentpagenum== #pageNum" >
<s:property/>
</s:if>
<s:else>
<s:url id= "page" action= "Pageaction!topage" >
<s:param name= "P.currentpagenum" >
<s:property value= "#pageNum"/>
</s:param>
<s:param name= "P.totalrecord" >
<s:property value= "#request. P.totalrecord"/>
</s:param>
</s:url>
<s:a href= "%{page}" ><s:property/></s:a>
</s:else>
</s:iterator>
</s:bean>
<s:url id= "Next" action= "Pageaction!topage" >
<s:param name= "P.currentpagenum" >
<s:property value= "#request. P.nextpagenum"/>
</s:param>
<s:param name= "P.totalrecord" >
<s:property value= "#request. P.totalrecord"/>
</s:param>
</s:url>
<s:a href= "%{next}" > next page </s:a>
<s:url id= "Lasturl" action= "Pageaction!topage" >
<s:param name= "P.currentpagenum" >
<s:property value= "#request. P.lastpagenum"/>
</s:param>
<s:param name= "P.totalrecord" >
<s:property value= "#request. P.totalrecord"/>
</s:param>
</s:url>
<s:a href= "%{lasturl}" > last page </s:a>
</div>