Implement a paging class using JAVA

Source: Internet
Author: User

Here, I use the example of querying user information from a user table to demonstrate its usage:

1. Compile the pageresultset. Java file into a class file and put it into your web
The package name can be modified under the WEB-INF/classes/COM/Youngor/util directory of the application.

2. In your action class:
First, extract data from the business processing logic class (arraylist or vector format)
Userbo = new userbo ();
Collection data = userbo. findusers (); // Sample Method
Then we can 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. On your JSP page:
<%
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 pagination toolbar -->

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

Note:
1. If you think the paging toolbar cannot meet your requirements, you can use the public method in the pageresultset class.
First (), previous (), next (), last () to customize their own toolbar, and you can also define multiple style toolbar in pageresultset;
2. The gettoolbar (string URL) method accepts parameters with query strings, such as "list_users.do? Class_id = 1 ".

// Pageresultset. Java
Package com. Youngor. util;

Import java. util .*;

/**
* <P> title: pageresultset </P>
*
* <P> Description: Paging class </p>
*
* <P> Copyright: Copyright (c) 2004 </p>
*
* <P> Company: youngor-studio (http://www.54youngor.com) </p>
* @ Author: Wu Weibo
* @ Version 1.0
*/
Public class PageResultSet {
/**
* Paging data
*/
Private Collection data = null;
/**
* Current page
*/
Private int curPage;
/**
* Number of records displayed on each page
*/
Private int pageSize;
/**
* Number of records
*/
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 page number.
*
* @ Return int
*/
Public int getcurpage (){
Return curpage;
}

/**
* Getpagesize: returns the page size.
*
* @ Return int
*/
Public int getpagesize (){
Return pageSize;
}

/**
* GetRowsCount: Total number of returned records
*
* @ Return int
*/
Public int getRowsCount (){
Return rowsCount;
}

/**
* GetPageCount: Total number of returned pages
*
* @ Return int
*/
Public int getPageCount (){
Return pageCount;
}
/**
* Page 1
* @ Return int
*/
Public int first (){
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;
}

/**
* Page 1
* @ Return boolean
*/
Public boolean isFirst (){
Return (curPage = 1 )? True: false;
}

/**
* Page 1
* @ Return boolean
*/
Public boolean isLast (){
Return (curPage = pageCount )? True: false;
}
/**
* Obtain the current page number.
* @ 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 + = "homepage Previous Page & nbsp ;";
Else
{
Str + = "<a href = '" + fileName + temp + "cur_page = 1'> homepage </a> & nbsp ;";
Str + = "<a href = '" + fileName + temp + "cur_page =" + (curPage-1) + "'> previous page </a> & nbsp ;";
}
If (isLast ())
Str + = "next last page & nbsp ;";
Else
{
Str + = "<a href = '" + fileName + temp + "cur_page =" + (curPage + 1) + "'> next page </a> & nbsp ;";
Str + = "<a href = '" + fileName + temp + "cur_page =" + pageCount + "'> last page </a> & nbsp ;";
}
Str + = "& nbsp; Total <B>" + rowsCount + "</B> Records & nbsp ;";
Str + = "& nbsp; 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> page" + I + "</option> ";
Else
Str + = "<option value = '" + I + "'> page" + I + "</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.