A Practical JSP paging code

Source: Internet
Author: User

Some enthusiastic netizens replied:
STR + = "go to <select name = 'page' onchange = \" window. location. href = '"+ filename + temp +" cur_page =' + this. options [this. selectedindex]. value \ "> ";
I already tried it. No problem

1. The following is a pagination class pageresultset.

CopyCode The Code is as follows: Package page. Bean;
Import java. util .*;
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;
}
/**
* Last Page
*
* @ Return Boolean
*/
Public Boolean islast (){
Return (curpage = pagecount )? True: false;
}
/*** Get the 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 + = "homepage previous ";
Else {
STR + = "<a href = '" + filename + temp + "cur_page = 1'> homepage </a> ";
STR + = "<a href = '" + filename + temp + "cur_page =" + (curpage-1) + "'> previous page </a> ";
}
If (islast ())
STR + = "next last page ";
Else {
STR + = "<a href = '" + filename + temp + "cur_page =" + (curpage + 1) + "'> next page </a> ";
STR + = "<a href = '" + filename + temp + "cur_page =" + pagecount + "'> last page </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> page" + I + "</option> ";
Else
STR + = "<option value = '" + I + "'> page" + I + "</option> ";
}
STR + = "</SELECT> </P> </form> ";
Return STR;
}
}

2. Here is action. Copy code The Code is as follows :/*
* Generated by myeclipse struts
* Template path: templates/Java/javaclass. VTL
*/
Package struts. Action;
Import java. util. collection;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. Apache. Struts. action. Action;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionforward;
Import org. Apache. Struts. Action. actionmapping;
Import page. Bean. databaseconn;
Import page. Bean. pageresultset;
Import page. Bean. contactbo;
Import struts. Form. loginform;
/**
* Myeclipse struts
* Creation date: 04-02-2008
*
* XDoclet definition:
* @ Struts. Action Path = "/login" name = "loginform" input = "/login. jsp" Scope = "request" Validate = "true"
* @ Struts. Action-forward name = "Sss" Path = "/index. jsp"
*/
Public class loginaction extends action {
/*
* Generated Methods
*/
/**
* Method Execute
* @ Param Mapping
* @ Param form
* @ Param request
* @ Param response
* @ Return actionforward
*/
Public actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response ){
Loginform = (loginform) form;
Contactbo userbo = new contactbo ();
// Extract data from the business processing logic class first (arraylist or vector format)
Collection data;
Try {
Data = userbo. findcontact (databaseconn. getconnection ());
// Obtain the current page curpage and the number of records per page pagesize.
// Int curpage = integer. parseint (string) request. getparameter ("cur_page "));
Int curpage = 1;
String cur = request. getparameter ("cur_page ");
System. Out. println ("--------------:" + cur );
If (cur! = NULL & cur! = ""){
Curpage = new INTEGER (cur). intvalue ();
}
Int pagesize = 10;
// Then generate the pageresultset object
Pageresultset datalist = new pageresultset (data, curpage, pagesize );
Request. setattribute ("userslist", datalist );
Return Mapping. findforward ("Sss ");
} Catch (exception e ){
E. printstacktrace ();
Return Mapping. getinputforward ();
}
}
}

3. The following page is displayed. Copy code The Code is as follows: <% @ page Language = "Java" Import = "Java. util. *, page. Bean. Contact, page. Bean. pageresultset"
Pageencoding = "gb2312" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + "://"
+ Request. getservername () + ":" + request. getserverport ()
+ Path + "/";
%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Title> my JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->
</Head>
<Body>
<Table border = "1">
<%
Pageresultset = (pageresultset) request. getattribute ("userslist ");
Arraylist userslist = (arraylist) pageresultset. getdata ();
For (INT I = 0; I <userslist. Size (); I ++ ){
Contact CO = (contact) userslist. Get (I );
%>
<Tr>
<TD>
<% = Co. GETID () %>
</TD>
<TD>
<A href = "login. do? Id = <% = Co. GETID () %> "> <% = Co. GetUserName () %> </a>
</TD>
<TD>
<% = Co. getmobile () %>
</TD>
<TD>
<% = Co. getmail () %>
</TD>
<TD>
<% = Co. getphone () %>
</TD>
<TD>
<% = Co. getmem () %>
</TD>
<TD>
<% = Co. getlastcontact () %>
</TD>
</Tr>
<%
}
%>
</Table>
<! -- Display pagination toolbar -->
<% = Pageresultset. gettoolbar ("login. Do") %>
</Body>
</Html>

Related Article

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.