A practical JSP paging code

Time:2021-9-20

Enthusiastic netizens replied:
STR + = “go to < select name =’page ‘onchange = \” window. Location. Href =’ “+ filename + temp +” cur_ page=’+this.options[this.selectedIndex].value\”>”;
Already tried, no problem

1. The following is the PageResultSet class that implements paging

Copy codeThe 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 per page
*/
private int pageSize;
/**
*Number of record lines
*/
private int rowsCount;
/**
*Number of pages
*/
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: returns the page size
*
* @return int
*/
public int getPageSize() {
return pageSize;
}
/**
*Getrowscount: returns the total number of record rows
*
* @return int
*/
public int getRowsCount() {
return rowsCount;
}
/**
*Getpagecount: returns the total number of pages
*
* @return int
*/
public int getPageCount() {
return pageCount;
}
/**
*First page
*
* @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;
}
/**
*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 toolbar
*
* @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 + = “home page Previous & nbsp;
else {
STR + = “< a href = ‘” + filename + temp + “cur_page = 1’ > Home Page < / a > & nbsp;
STR + = “< a href = ‘” + filename + temp + “cur_page =” + (curpage – 1) + “> previous page < / a > & nbsp;
}
if (isLast())
STR + = “end of next 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;
}
}

2. Next is action

Copy codeThe 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 = (LoginForm) form;
ContactBO userBO=new ContactBO();
//First, get the data from the business processing 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;
//The PageResultSet object is then generated
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 pagination

Copy codeThe 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 = (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>
<!– Show paging toolbar — >
<%=pageResultSet.getToolBar(“login.do”)%>
</body>
</html>