Implementing a paging class in Java

Source: Internet
Author: User
Tags prev
Paging Here I use the example of querying user information from a user table to illustrate its usage:

1. Compile the Pageresultset.java file into a class file and put it into your web
In the Web-inf/classes/com/youngor/util directory of the application, you can modify the package name accordingly.

2. In your action class:
First remove the data from the business Process logic class (ArrayList or vector format)
Userbo userbo=new Userbo ();
Collection data=userbo.findusers ();//Example method
Then get the current page curpage and the number of records per page pagesize
int curpage = Integer.parseint (Request.getparameter ("Cur_page"));
int pagesize=15;
Then generate the Pageresultset object
Pageresultset dataList = new Pageresultset (data, curpage, pageSize);
Request.setattribute ("Userslist", userslist);

3. In your JSP page:
<%
Pageresultset pageresultset= (Pageresultset) request.getattribute ("Userslist");
ArrayList userslist= (ArrayList) pageresultset.getdata ();
for (int i=0;i<userslist.size (); i++)
{
Usereo usereo= (Usereo) userslist.get (i);%>
<tr>
<td><a href= "View_user.do?id=<%=usereo.getid ()%>" ><%=usereo.getusername ()%></a> </td>
<td><%=usereo.getname ()%></td>
<td><%=usereo.getphonenumber ()%></td>
<td><%=usereo.getemailbox ()%></td>
<td><%=usereo.getaddress ()%></td>
<td><%=usereo.getpostcode ()%></td>
</tr>
<%}%>
</table></td>
</tr>
</table>

<!--display the page---> toolbar

<%=pageresultset.gettoolbar ("list_users.do")%>


Attention:
1. If you feel that the paging toolbar does not meet your requirements, you can use the public method in the Pageresultset class
The one (), previous (), Next (), last () Customize your own toolbars, and you can also define multiple style toolbars in Pageresultset.
2. The Gettoolbar (string url) method accepts parameters with a query string, such as "list_users.do?class_id=1".



Pageresultset.java
Package com.youngor.util;

Import java.util.*;

/**
* <p>Title:PageResultSet</p>
*
* <p>description: Pagination class </p>
*
* <p>copyright:copyright (c) 2004</p>
*
* <p>company:youngor-studio (http://www.54youngor.com) </p>
* @author: Woovipo
* @version 1.0
*/
public class Pageresultset {
/**
* Paging Data
*/
Private Collection data = null;
/**
* Current Page
*/
private int curpage;
/**
* Number of records displayed per page
*/
private int pageSize;
/**
* Record number of rows
*/
private int rowscount;
/**
* Page number
*/
private int PageCount;

Public Pageresultset (Collection data) {
This.data = data;
This.curpage = 1;
This.pagesize = 10;
This.rowscount = Data.size ();
This.pagecount = (int) Math.ceil (double) rowscount/pagesize);
}

Public Pageresultset (Collection data, int curpage) {
This.data = data;
This.curpage = Curpage;
This.pagesize = 10;
This.rowscount = Data.size ();
This.pagecount = (int) Math.ceil (double) rowscount/pagesize);
}

Public Pageresultset (Collection data, int curpage, int pageSize) {
This.data = data;
This.curpage = Curpage;
This.pagesize = pageSize;
This.rowscount = Data.size ();
This.pagecount = (int) Math.ceil (double) rowscount/pagesize);
}

/**
* Getcurpage: Returns the current number of pages
*
* @return int
*/
public int getcurpage () {
return curpage;
}

/**
* GetPageSize: Back to paging size
*
* @return int
*/
public int getpagesize () {
return pageSize;
}

/**
* Getrowscount: Returns the total number of record rows
*
* @return int
*/
public int Getrowscount () {
return rowscount;
}

/**
* Getpagecount: Return total pages
*
* @return int
*/
public int Getpagecount () {
return PageCount;
}
/**
* First Page
* @return int
*/
public int-A () {
return 1;
}
/**
* Last Page
* @return int
*/
public int-Last () {
return PageCount;
}
/**
* Previous Page
* @return int
*/
public int Previous () {
Return (CurPage-1 < 1)? 1:curpage-1;
}
/**
* Next Page
* @return int
*/
public int Next () {
Return (Curpage + 1 > PageCount)? Pagecount:curpage + 1;
}

/**
* First Page
* @return Boolean
*/
public Boolean Isfirst () {
Return (curpage==1) True:false;
}

/**
* First Page
* @return Boolean
*/
public Boolean islast () {
Return (Curpage==pagecount) True:false;
}
/**
* Get current page data
* @return Collection
*/
Public Collection GetData () {
Collection curdata = null;
if (data!= null) {
int start = (curPage-1) * pageSize;
int end = 0;
if (start + pageSize > Rowscount)
end = Rowscount;
Else
End = start + pageSize;
ArrayList arraycurdata = new ArrayList ();
ArrayList arraydata = null;
Vector vectorcurdata = new vector ();
Vector vectordata = null;
Boolean IsArray = true;
if (data instanceof ArrayList) {
Arraydata = (ArrayList) data;
IsArray = true;
else if (data instanceof Vector) {
Vectordata = (Vector) data;
IsArray = false;
}
for (int i = start; i < end; i++) {
if (IsArray) {
Arraycurdata.add (Arraydata.get (i));
} else {
Vectordata.add (Vectordata.elementat (i));
}
}
if (IsArray) {
Curdata = (Collection) arraycurdata;
} else {
Curdata = (Collection) vectorcurdata;
}
}
return curdata;
}
/**
* Get tool bar
* @return String
*/
public string Gettoolbar (string fileName) {
String temp= "";
if (Filename.indexof ("?") ==-1)
{
Temp= "?";
}
Else
{
temp= "&";
}
String str= "<form method= ' post ' name= ' frmpage ' action= '" "+filename+" ' > ";
str+= "<p align= ' center ' >";
if (Isfirst ())
str+= "First Prev";
Else
{
str+= "<a href= '" +filename+temp+ "cur_page=1 ' > Home </a>";
str+= "<a href= '" +filename+temp+ "cur_page=" + (curPage-1) + "' > Prev </a>";
}
if (Islast ())
str+= "next Page last";
Else
{
str+= "<a href= '" +filename+temp+ "cur_page=" + (curpage+1) + "' > Next </a>";
str+= "<a href= '" +filename+temp+ "cur_page=" +pagecount+ "' > End </a>";
}
str+= "Total <b>" +rowscount+ "</b> Records";
str+= "Go to <select name= ' page ' onchange=\" location= ' "+filename+temp+" cur_page= ' +this.options[this.selectedindex] . value\ ">";
for (int i=1;i<=pagecount;i++)
{
if (i==curpage)
str+= "<option value= '" +i+ "selected>" +i+ "page </option>";
Else
str+= "<option value= '" +i+ "' >" +i+ "page </option>";
}
str+= "</select></p></form>";
return str;
}
}




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.