A practical JSP paging code _JSP programming

Source: Internet
Author: User
Tags prev

Have enthusiastic Netizen reply:
str = "Go to <select name= ' page ' onchange=\" window.location.href= ' "+ fileName + temp +" cur_page= "+this.options[this.se Lectedindex].value\ ">";
I've tried, no problem.

1. The following is the implementation of the paging class Pageresultset

Copy Code code 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 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;
}
/**
* Last 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 Last";
else {
STR + + "<a href= '" + fileName + temp + "cur_page=" + (Curpage + 1) + "' > Next </a>";
STR + + "<a href= '" + fileName + temp + "cur_page=" + PageCount + "' > Last </a>";
}
STR + + "Total <b>" + Rowscount + "</b> record";
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> First" + i + "page </option>";
Else
STR + + "<option value= '" + i + "' > first" + i + "page </option>";
}
STR + + "</select></p></form>";
return str;
}
}

2. It's action.
Copy Code code 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 = (loginform) Form;
Contactbo userbo=new Contactbo ();
First remove the data from the business Process logic class (ArrayList or vector format)
Collection data;
try {
data = Userbo.findcontact (Databaseconn.getconnection ());
Then get 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 is the page showing the pagination
Copy Code code 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" >
<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 are my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-->
<body>
<table border= "1" >
<%
Pageresultset 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 the page---> toolbar
<%=pageresultset.gettoolbar ("login.do")%>
</body>

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.