Write a paging class and share it with everyone
Available in two ways one is home, Prev, Next, last, and the other is digital
Package com.demo.util;
Import Javax.servlet.http.HttpServletRequest;
/***
* Paging operation
*
* @author Yaa
*
*/
public class Pagnation {
Total Records
private int totalrecordsize = 0;
Total page numbers
private int totalpagesize = 0;
Current page number
private int currentpage = 1;
Number of record bars to display per page
private int pageSize = 20;
The URL address of the page to manipulate
Private String pageurl = "";
Customizing the parameter string to pass
Private String queryString = "";
Number of pages to display before and after
private int spacesize = 5;
public int getspacesize () {
return spacesize;
}
public void setspacesize (int spacesize) {
This.spacesize = spacesize;
}
public int gettotalrecordsize () {
return totalrecordsize;
}
public void settotalrecordsize (int totalrecordsize) {
This.totalrecordsize = totalrecordsize;
}
public int gettotalpagesize () {
return totalpagesize;
}
public void settotalpagesize (int totalpagesize) {
This.totalpagesize = totalpagesize;
}
public int getcurrentpage () {
return currentpage;
}
public void setcurrentpage (int currentpage) {
This.currentpage = CurrentPage;
}
public int getpagesize () {
return pageSize;
}
public void setpagesize (int pageSize) {
This.pagesize = pageSize;
}
Public String Getpageurl () {
return pageurl;
}
public void Setpageurl (String pageurl) {
This.pageurl = Pageurl;
}
Public String getquerystring () {
return queryString;
}
public void setquerystring (String queryString) {
this.querystring = queryString;
}
/**
* Construction Method
*
* @param totalrecordsize
* Total amount recorded
* @param request
* HttpServletRequest
*/
Public pagnation (int totalrecordsize, httpservletrequest request) {
This.totalrecordsize = totalrecordsize;
if (Request.getparameter ("page")! = null) {
try {
This.currentpage = Integer.parseint (Request
. GetParameter ("page"). toString ());
} catch (NumberFormatException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
/**
* Construction Method
*
* @param totalrecordsize
* Total amount recorded
* @param request
* HttpServletRequest
* @param pageurl
* URL address of the page action
*/
Public pagnation (int totalrecordsize, httpservletrequest request,
String pageurl) {
This.totalrecordsize = totalrecordsize;
if (Request.getparameter ("page")! = null) {
try {
This.currentpage = Integer.parseint (Request
. GetParameter ("page"). toString ());
} catch (NumberFormatException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
This.pageurl = Pageurl;
}
/***
* Get page-generated strings
*
* @return
*/
Public String getpagnationstring () {
this.totalpagesize = (int) Math.ceil (this.totalrecordsize
/Math.floor (this.pagesize));
if (This.currentpage < 1) {
This.currentpage = 1;
}
if (This.currentpage > This.totalpagesize && this.totalpagesize! = 0) {
This.currentpage = this.totalpagesize;
}
Paging Information generation
String componentstr = Componentrender ();
Paging generation
Return Pagerender (COMPONENTSTR);
}
/**
* Paging Information generation
*
* @param SB
*/
private string Pagerender (string componentstr) {
StringBuffer sb = new StringBuffer ();
Sb
. Append (
"<table width=\" 100%\ "border=\" 0\ "cellspacing=\" 0\ "cellpadding=\" 3\ ">")
. Append (
"<tr><td width= ' ></td><td class= ' zi17 ' align=\" right\ ">")
. Append (Componentstr). Append ("</td>"). Append (
"<td class= ' zi17 ' align=\" left\ ">"). Append ("Total")
. Append (This.totalrecordsize). Append ("records"). Append ("Total")
. Append (This.totalpagesize). Append ("page"). Append ("current section")
. Append (This.currentpage). Append ("page")
. Append ("<input type= ' text ' id= ' pagnationnum ' value= ' 1 ' style= ' width:12px;height:12px '/> '). Append (" <input Type= ' button ' style= ' width:20px;height:20px ' value= ' Go ' onclick= ' gopage () '/> ')
. Append ("<td></td> </tr>"). Append ("</table>")
. Append ("<script type= ' Text/javascript ' >")
. Append ("function gopage ()")
. Append ("{tmpvalue =document.getelementbyid (' Pagnationnum '). Value;")
. Append ("if (IsNaN (Tmpvalue)) {document.getElementById (' Pagnationnum '). value= ' 1 '; return;}")
. Append ("window.location.href=" "+this.pageurl+"? page= ' +tmpvalue; ")
. Append ("}")
. Append ("</script>");
return sb.tostring ();
}
/**
* Paging Generation
*
* @param SB
*/
Private String Componentrender () {
StringBuffer sb = new StringBuffer ();
If the current page number is greater than 1
if (This.currentpage > 1) {
Sb.append ("[<a href= '" + This.pageurl + "? page=1");
Sb.append (this.querystring);
Sb.append ("' Class=\" \ ">");
Sb.append ("Home");
Sb.append ("</A>] [<a href= '" + This.pageurl + "? page=");
Sb.append (currentPage-1);
Sb.append (this.querystring);
Sb.append ("' Class=\" \ ">");
Sb.append ("previous page");
Sb.append ("</A>]");
} else {//The current page number is less than 1
Sb.append ("[Home] [prev]");
}
If the current page number is less than the total page
if (This.currentpage < this.totalpagesize) {
Sb.append ("<a href=" + This.pageurl + "? page=");
Sb.append (currentpage + 1);
Sb.append (this.querystring);
Sb.append ("' Class=\" \ ">");
Sb.append ("[Next Page]");
Sb.append ("</a> [<a href= '" + This.pageurl + "? page=");
Sb.append (this.totalpagesize);
Sb.append (this.querystring);
Sb.append ("' Class=\" \ ">");
Sb.append ("last");
Sb.append ("</A>]");
} else {//The current page number is greater than the total page number
Sb.append ("[Next page] [last]");
}
return sb.tostring ();
}
//<summary>
//Set paging content
//</summary>
& nbsp <param name= "PageIndex" > Current page </param>
//<param name= "PageSize" > Number of information bars per page </param>
//<param name= "pageSize" > Current page URL address </param>
//<returns> pagination Content Object </returns>
public String getnumpagestring ()
{
this.totalpagesize = (int) math.ceil (this.totalrecordsize
/Math.floor (this.pagesize));
if (This.currentpage < 1) {
this.currentpage = 1;
}
; if (this.currentpage > This.totalpagesize && this.totalpagesize! = 0) {
this.cu Rrentpage = this.totalpagesize;
}
String pagefirststr = "page number:";
int preindex = 0;
int endIndex = 0;
Preindex = (currentpage-spacesize) < 1? 1:currentpage-spacesize;
EndIndex = (currentpage + spacesize < Spacesize * 2? Spacesize * 2: (currentpage + spacesize)) > totalpagesize? Totalpagesize:currentpage + spacesize;
for (int i = preIndex-1; i < EndIndex; i++)
{
if ((i + 1) = = CurrentPage)
{
Pagefirststr + = " <b><font color= ' #ff0000 ' >" + currentpage + "</font></b>";
}
Else
{
Pagefirststr + = " <a href=" + This.pageurl + "? page=" + (i + 1) + "+ this.querystring +" ' > "+ (i + 1) + "</a>";
}
}
int tonum = 0;
if (currentpage = = totalpagesize)
{
Tonum = totalrecordsize% PageSize = = 0? Pagesize:totalrecordsize% PageSize;
}
Else
{
Tonum = pageSize;
}
Pagefirststr + = " Total" + totalrecordsize + "record, per page" + pageSize + "bar, total" + totalpagesize + "page, current" + CurrentPage + " "Page," + ((currentPage-1) * pageSize + 1) + "bar to" + ((currentPage-1) * pageSize + tonum) + "bar. ";
String pagesecondstr = " jump to: <input type= ' text ' id= ' page ' class= ' input ' onblur= ' chknum (this) ' size= ' 3 ' Value= ' 1 ' > page <input class= ' btn ' Type=button value= jump onclick=\ "window.location.href= '" + This.pageurl + "? page= ' +document.getelementbyid (' page '). value+ '" + this.querystring + "'; \" ><input type= ' Hidden ' id= ' totalpagesize ' value= ' + totalpagesize + ' > ';
StringBuilder sbtable = new StringBuilder ();
Sbtable.append ("<table id=\" pagation\ "width=\" 100%\ "");
Sbtable.append ("<tr id=\" Pa\ ">");
Sbtable.append (PAGEFIRSTSTR);
Sbtable.append ("<td align=\" left\ "height=\" 30px\ ">");
Sbtable.append ("</td>");
Sbtable.append ("<td align=\" right\ "height=\" 30px\ ">");
Sbtable.append (PAGESECONDSTR);
Sbtable.append ("</td>");
Sbtable.append ("</tr>");
Sbtable.append ("</table>");
return sbtable.tostring ();
}
}
How to use
In the action-corresponding method, add
Paging operations
Pagnation pagnation = new Pagnation (totalcount, request, URL);
TotalCount//Total data
Request
URL//Address to be paged
There are other properties that may be used, already in the pagination class.
Pagnation.getpagnationstring ()//Get page-generated string
To define a PAGESTR field string in the action class, set the Set,get method to assemble the resulting string.
You can use the Struts2 label <s:property value= "Pagestr" escape= "false"/> on JSP pages