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