• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

一个实用的JSP分页代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

有热心网友回复:
str += " 转到<select name='page' onChange=\"window.location.href='" + fileName + temp + "cur_page='+this.options[this.selectedIndex].value\">";
已经试过了,没问题

1.以下是实现分页的类PageResultSet

复制代码 代码如下:

package page.bean;
import java.util.*;
public class PageResultSet {
/**
* 分页数据
*/
private Collection data = null;
/**
* 当前页
*/
private int curPage;
/**
* 每页显示的记录数
*/
private int pageSize;
/**
* 记录行数
*/
private int rowsCount;
/**
* 页数
*/
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:返回当前的页数
*
* @return int
*/
public int getCurPage() {
return curPage;
}
/**
* getPageSize:返回分页大小
*
* @return int
*/
public int getPageSize() {
return pageSize;
}
/**
* getRowsCount:返回总记录行数
*
* @return int
*/
public int getRowsCount() {
return rowsCount;
}
/**
* getPageCount:返回总页数
*
* @return int
*/
public int getPageCount() {
return pageCount;
}
/**
* 第一页
*
* @return int
*/
public int first() {
return 1;
}
/**
* 最后一页
*
* @return int
*/
public int last() {
return pageCount;
}
/**
* 上一页
*
* @return int
*/
public int previous() {
return (curPage - 1 < 1) ? 1 : curPage - 1;
}
/**
* 下一页
*
* @return int
*/
public int next() {
return (curPage + 1 > pageCount) ? pageCount : curPage + 1;
}
/**
* 第一页
*
* @return boolean
*/
public boolean isFirst() {
return (curPage == 1) ? true : false;
}
/**
* 最后一页
*
* @return boolean
*/
public boolean isLast() {
return (curPage == pageCount) ? true : false;
}
/** * 获取当前页数据
*
* @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;
}
/**
* 获取工具条
*
* @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 += "首页 上一页&nbsp;";
else {
str += "<a href='" + fileName + temp + "cur_page=1'>首页</a>&nbsp;";
str += "<a href='" + fileName + temp + "cur_page=" + (curPage - 1) + "'>上一页</a>&nbsp;";
}
if (isLast())
str += "下一页 尾页&nbsp;";
else {
str += "<a href='" + fileName + temp + "cur_page=" + (curPage + 1) + "'>下一页</a>&nbsp;";
str += "<a href='" + fileName + temp + "cur_page=" + pageCount + "'>尾页</a>&nbsp;";
}
str += "&nbsp;共<b>" + rowsCount + "</b>条记录&nbsp;";
str += "&nbsp;转到<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 + "页</option>";
else
str += "<option value='" + i + "'>第" + i + "页</option>";
}
str += "</select></p></form>";
return str;
}
}

2.一下是Action
复制代码 代码如下:

/*
* 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();
//先从业务处理逻辑类中取出数据(ArrayList或Vector格式)
Collection data;
try {
data = userBO.findContact(DatabaseConn.getConnection());
//再得到当前页curPage和每页记录数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;
//然后生成PageResultSet对象
PageResultSet dataList = new PageResultSet(data, curPage, pageSize);
request.setAttribute("usersList", dataList);
return mapping.findForward("sss");
} catch (Exception e) {
e.printStackTrace();
return mapping.getInputForward();
}
}
}

3.以下是显示分页的页面
复制代码 代码如下:

<%@ 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>
<!-- 显示分页工具栏 -->
<%=pageResultSet.getToolBar("login.do")%>
</body>
</html>


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
jsp ${param.id}用法发布时间:2022-02-05
下一篇:
jsp hibernate的分页代码第1/3页发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap