jsp分页技术
2013-06-11 21:08
211 查看
下面是Pages.java的代码
package com.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
public class Pages<T> {
private int rowCount = 10; // 当页显示记录数
private int pageCount = 1; // 总页数
private int currentPage = 1; // 当前页
private int sPage = 1; // 当前页开始记录数
private long total = 1; // 总记录数
private String pageStr = ""; // 分页字符串
public Pages() {
}
private List<T> list = null;
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public Pages(HttpServletRequest request) {
// 获得当前页
String cPageStr = request.getParameter("requestPage") == null ? String
.valueOf(currentPage) : request.getParameter("requestPage");
String rowCountStr = request.getParameter("rowCount") == null ? String
.valueOf(rowCount) : request.getParameter("rowCount");
int cPageInt = 0;
try {
cPageInt = Integer.parseInt(cPageStr);
} catch (Exception e) {
cPageInt = 1;
} finally {
currentPage = cPageInt;
}
int rowCountInt = 0;
try {
rowCountInt = Integer.parseInt(rowCountStr);
} catch (Exception e) {
rowCountInt = 1;
} finally {
rowCount = rowCountInt;
}
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int page) {
if (page <= 0) {
page = 1;
} else
currentPage = page;
}
public String getPageStr() {
return pageStr;
}
public void setPageStr(String pageStr) {
this.pageStr = pageStr;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getSPage() {
return sPage;
}
public void setSPage(int page) {
this.sPage = page;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public String wrapSqlTotal(String sql, Connection conn, Object[] obj) {
String str = "select count(*) from (" + sql + ") tab";
try {
PreparedStatement pst = conn.prepareStatement(str);
ResultSet rs = pst.executeQuery();
if (obj != null && obj.length > 0) {
for (int i = 0; i < obj.length; i++) {
pst.setObject(i + 1, obj[i]);
}
}
while (rs.next()) {
this.total = rs.getInt(1);
}
StringBuffer sb = new StringBuffer();
sb.append("select * from ( select row_number() over(order by id ) as rownum,* from ("
+ sql
+ ") tab ) as result "
+ " where result.rownum between "
+ ((currentPage - 1) * rowCount + 1)
+ " and "
+ currentPage * rowCount);
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String wrapOracleTotal(String sql, Connection conn, Object[] obj) {
String str = "select count(*) from (" + sql + ") tab";
try {
PreparedStatement pst = conn.prepareStatement(str);
ResultSet rs = pst.executeQuery();
if (obj != null && obj.length > 0) {
for (int i = 0; i < obj.length; i++) {
pst.setObject(i + 1, obj[i]);
}
}
while (rs.next()) {
this.total = rs.getInt(1);
}
StringBuffer sb = new StringBuffer();
sb.append("SELECT * FROM ( SELECT A.*, ROWNUM RN " + " FROM ("
+ sql + " order by id) A )" + " WHERE RN BETWEEN "
+ ((currentPage - 1) * rowCount + 1) + " AND "
+ currentPage * rowCount);
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String wrapMySqlTotal(String sql, Connection conn, Object[] obj) {
String str = "select count(*) from (" + sql + ") tab";
try {
PreparedStatement pst = conn.prepareStatement(str);
ResultSet rs = pst.executeQuery();
if (obj != null && obj.length > 0) {
for (int i = 0; i < obj.length; i++) {
pst.setObject(i + 1, obj[i]);
}
}
while (rs.next()) {
this.total = rs.getInt(1);
}
StringBuffer sb = new StringBuffer();
sb.append(sql)
.append(" ")
.append(" limit " + (currentPage - 1) * rowCount + ","
+ rowCount);
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
这是dao层测试的方法
public List<User> query(Pages<User> pages){
List<User> list=new ArrayList<User>();
Connection conn = JdbcUtils.getConnection();
String sql = "select * from t_user";
try {
// sql = pages.wrapSqlTotal(sql, conn, null);
// sql=pages.wrapMySqlTotal(sql, conn, null);
sql=pages.wrapOracleTotal(sql, conn, null);
System.out.println(sql);
list = (List<User>) JdbcUtils.getQueryRunner().query(conn, sql,
new BeanListHandler(User.class));
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
下面是servlet端的部分代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Pages<User> pages=new Pages<User>(request);
UserDao userDao=new UserDao();
List<User> list=userDao.query(pages);
request.setAttribute("list", list);
request.setAttribute("pages", pages);
request.getRequestDispatcher("/testpage.jsp").forward(request, response);
}
下面是jsp页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="pager.js"></script>
</head>
<body>
<form id="f" name="f" method="post" action="${pageContext.request.contextPath }/PageServlet">
<input type="hidden" name="requestPage" id="requestPage" />
<input type="hidden" name="rowCount" id="rowCount" value="10" />
<table width="70%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>学号</td>
<td>姓名</td>
</tr>
<c:forEach items="${list}" var="u" varStatus="stat">
<c:set var="cs" value="background-color:#D3E6FC;"></c:set>
<c:if test="${stat.index % 2 == 0}">
<c:set var="cs" value="background-color:#DEDFDE;"></c:set>
</c:if>
<tr>
<td style="width:50px;${cs}">${u.id}</td>
<td style="width:50px;${cs}">${u.name}</td>
</tr>
</c:forEach>
</table>
<script type="text/javascript">
//此段js写在表格下方
var pager= new Pager(document.f,${pages.total},${pages.rowCount},${pages.currentPage});//第一个参数是查询列表的form,第二个是总记录,第三个是每页显示记录数,第四个是当前第几页
document.write(pager.toString());
function query(){
document.f.submit();
}
</script>
</form>
</body>
</html>
下面是pager.js脚本代码
function Pager(form, recordCount, pageSize, currentPage, lang) {
this.form = form;
this.recordCount = recordCount;
this.pageSize = pageSize;
$("#rowCount").val(pageSize);
this.currentPage = currentPage;
$("#requestPage").val(currentPage);
this.lang = lang || "cn" ;
}
Pager.prototype.gotoPage = function(page) {
var pageSize = $("#selectRowCount").val();
$("#rowCount").val(pageSize);
this.form.requestPage.value = page;
query();
};
Pager.prototype.setRowCount = function()
{
var pageSize = $("#selectRowCount").val();
$("#rowCount").val(pageSize);
query();
}
/**
* pager string
*/
Pager.prototype.toString = function (){
var pager = this;
var pagerStrings = [];
var pageCount = Math.ceil(this.recordCount / this.pageSize);
var previousPage = this.currentPage - 1;
var firstPage = 1;
var nextPage = this.currentPage + 1;
var lastPage = pageCount;
pagerStrings[pagerStrings.length] = '\u5171<span style="color:#FF0000"> ' + this.recordCount + ' </span>\u6761\u8bb0\u5f55 ';
pagerStrings[pagerStrings.length] = '\u6bcf\u9875\u663e\u793a<span style="color:#FF0000"> ';
pagerStrings[pagerStrings.length] = '<select id="selectRowCount" onchange="pager.setRowCount()"><option value="10" ';
if (this.pageSize == 10)
{
pagerStrings[pagerStrings.length] = 'selected';
}
pagerStrings[pagerStrings.length] ='>10</option><option value="20" ';
if (this.pageSize == 20)
{
pagerStrings[pagerStrings.length] = 'selected';
}
pagerStrings[pagerStrings.length] ='>20</option><option value="50" ';
if (this.pageSize == 50)
{
pagerStrings[pagerStrings.length] = 'selected';
}
pagerStrings[pagerStrings.length] ='>50</option><option value="100" ';
if (this.pageSize == 100)
{
pagerStrings[pagerStrings.length] = 'selected';
}
pagerStrings[pagerStrings.length] = '>100</option></select>';
pagerStrings[pagerStrings.length] = ' </span>\u6761 \u5206<span style="color:#FF0000"> ' + pageCount + ' </span>\u9875 ';
if (this.currentPage == 1 || pageCount <= 1) {
pagerStrings[pagerStrings.length] = '<span style="color:#CCCCCC;"> \u9996\u9875 \u4e0a\u4e00\u9875 </span>';
} else {
pagerStrings[pagerStrings.length] = '<span style="cursor:pointer;text-decoration: underline;" onclick="pager.gotoPage(1);">\u9996\u9875</span> <span style="cursor:pointer;text-decoration: underline;" onclick="pager.gotoPage('
+ previousPage + ');">\u4e0a\u4e00\u9875</span> ';
}
if (this.currentPage == pageCount || pageCount <= 1) {
pagerStrings[pagerStrings.length] = '<span style="color:#CCCCCC;"> \u4e0b\u4e00\u9875 \u5c3e\u9875</span>';
} else {
pagerStrings[pagerStrings.length] = '<span style="cursor:pointer;text-decoration: underline;" onclick="pager.gotoPage(' + nextPage + ');">\u4e0b\u4e00\u9875</span> <span style="cursor:pointer;text-decoration: underline;"
onclick="pager.gotoPage(' + lastPage + ');">\u5c3e\u9875</span> ';
}
pagerStrings[pagerStrings.length] = ' \u7b2c <input style="text-align:center;border: 1px solid #CCCCCC;" name="pager.requestPage" type="text" id="reqPage" size="4" maxlength="6" value="' + this.currentPage + '" onchange="this.value
= isInteger(this.value) ? this.value : 1" /> \u9875 ';
pagerStrings[pagerStrings.length] = '<input name="buttonsubmitPage" type="button" id="buttonsubmitPage" value="\u8f6c\u5230" onclick="pager.gotoPage($(\'#reqPage\').attr(\'value\'));" />';
return pagerStrings.join("").toString();
};
function isInteger(value) {
return /^[-\+]?\d+$/.test(value)
};
function showPaginIcon(form,recordCount,pageSize,currentPage){
var pager = new Pager(form,recordCount,pageSize,currentPage);
document.write(pager.toString());
}
package com.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
public class Pages<T> {
private int rowCount = 10; // 当页显示记录数
private int pageCount = 1; // 总页数
private int currentPage = 1; // 当前页
private int sPage = 1; // 当前页开始记录数
private long total = 1; // 总记录数
private String pageStr = ""; // 分页字符串
public Pages() {
}
private List<T> list = null;
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public Pages(HttpServletRequest request) {
// 获得当前页
String cPageStr = request.getParameter("requestPage") == null ? String
.valueOf(currentPage) : request.getParameter("requestPage");
String rowCountStr = request.getParameter("rowCount") == null ? String
.valueOf(rowCount) : request.getParameter("rowCount");
int cPageInt = 0;
try {
cPageInt = Integer.parseInt(cPageStr);
} catch (Exception e) {
cPageInt = 1;
} finally {
currentPage = cPageInt;
}
int rowCountInt = 0;
try {
rowCountInt = Integer.parseInt(rowCountStr);
} catch (Exception e) {
rowCountInt = 1;
} finally {
rowCount = rowCountInt;
}
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int page) {
if (page <= 0) {
page = 1;
} else
currentPage = page;
}
public String getPageStr() {
return pageStr;
}
public void setPageStr(String pageStr) {
this.pageStr = pageStr;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getSPage() {
return sPage;
}
public void setSPage(int page) {
this.sPage = page;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public String wrapSqlTotal(String sql, Connection conn, Object[] obj) {
String str = "select count(*) from (" + sql + ") tab";
try {
PreparedStatement pst = conn.prepareStatement(str);
ResultSet rs = pst.executeQuery();
if (obj != null && obj.length > 0) {
for (int i = 0; i < obj.length; i++) {
pst.setObject(i + 1, obj[i]);
}
}
while (rs.next()) {
this.total = rs.getInt(1);
}
StringBuffer sb = new StringBuffer();
sb.append("select * from ( select row_number() over(order by id ) as rownum,* from ("
+ sql
+ ") tab ) as result "
+ " where result.rownum between "
+ ((currentPage - 1) * rowCount + 1)
+ " and "
+ currentPage * rowCount);
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String wrapOracleTotal(String sql, Connection conn, Object[] obj) {
String str = "select count(*) from (" + sql + ") tab";
try {
PreparedStatement pst = conn.prepareStatement(str);
ResultSet rs = pst.executeQuery();
if (obj != null && obj.length > 0) {
for (int i = 0; i < obj.length; i++) {
pst.setObject(i + 1, obj[i]);
}
}
while (rs.next()) {
this.total = rs.getInt(1);
}
StringBuffer sb = new StringBuffer();
sb.append("SELECT * FROM ( SELECT A.*, ROWNUM RN " + " FROM ("
+ sql + " order by id) A )" + " WHERE RN BETWEEN "
+ ((currentPage - 1) * rowCount + 1) + " AND "
+ currentPage * rowCount);
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String wrapMySqlTotal(String sql, Connection conn, Object[] obj) {
String str = "select count(*) from (" + sql + ") tab";
try {
PreparedStatement pst = conn.prepareStatement(str);
ResultSet rs = pst.executeQuery();
if (obj != null && obj.length > 0) {
for (int i = 0; i < obj.length; i++) {
pst.setObject(i + 1, obj[i]);
}
}
while (rs.next()) {
this.total = rs.getInt(1);
}
StringBuffer sb = new StringBuffer();
sb.append(sql)
.append(" ")
.append(" limit " + (currentPage - 1) * rowCount + ","
+ rowCount);
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
这是dao层测试的方法
public List<User> query(Pages<User> pages){
List<User> list=new ArrayList<User>();
Connection conn = JdbcUtils.getConnection();
String sql = "select * from t_user";
try {
// sql = pages.wrapSqlTotal(sql, conn, null);
// sql=pages.wrapMySqlTotal(sql, conn, null);
sql=pages.wrapOracleTotal(sql, conn, null);
System.out.println(sql);
list = (List<User>) JdbcUtils.getQueryRunner().query(conn, sql,
new BeanListHandler(User.class));
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
下面是servlet端的部分代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Pages<User> pages=new Pages<User>(request);
UserDao userDao=new UserDao();
List<User> list=userDao.query(pages);
request.setAttribute("list", list);
request.setAttribute("pages", pages);
request.getRequestDispatcher("/testpage.jsp").forward(request, response);
}
下面是jsp页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="pager.js"></script>
</head>
<body>
<form id="f" name="f" method="post" action="${pageContext.request.contextPath }/PageServlet">
<input type="hidden" name="requestPage" id="requestPage" />
<input type="hidden" name="rowCount" id="rowCount" value="10" />
<table width="70%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>学号</td>
<td>姓名</td>
</tr>
<c:forEach items="${list}" var="u" varStatus="stat">
<c:set var="cs" value="background-color:#D3E6FC;"></c:set>
<c:if test="${stat.index % 2 == 0}">
<c:set var="cs" value="background-color:#DEDFDE;"></c:set>
</c:if>
<tr>
<td style="width:50px;${cs}">${u.id}</td>
<td style="width:50px;${cs}">${u.name}</td>
</tr>
</c:forEach>
</table>
<script type="text/javascript">
//此段js写在表格下方
var pager= new Pager(document.f,${pages.total},${pages.rowCount},${pages.currentPage});//第一个参数是查询列表的form,第二个是总记录,第三个是每页显示记录数,第四个是当前第几页
document.write(pager.toString());
function query(){
document.f.submit();
}
</script>
</form>
</body>
</html>
下面是pager.js脚本代码
function Pager(form, recordCount, pageSize, currentPage, lang) {
this.form = form;
this.recordCount = recordCount;
this.pageSize = pageSize;
$("#rowCount").val(pageSize);
this.currentPage = currentPage;
$("#requestPage").val(currentPage);
this.lang = lang || "cn" ;
}
Pager.prototype.gotoPage = function(page) {
var pageSize = $("#selectRowCount").val();
$("#rowCount").val(pageSize);
this.form.requestPage.value = page;
query();
};
Pager.prototype.setRowCount = function()
{
var pageSize = $("#selectRowCount").val();
$("#rowCount").val(pageSize);
query();
}
/**
* pager string
*/
Pager.prototype.toString = function (){
var pager = this;
var pagerStrings = [];
var pageCount = Math.ceil(this.recordCount / this.pageSize);
var previousPage = this.currentPage - 1;
var firstPage = 1;
var nextPage = this.currentPage + 1;
var lastPage = pageCount;
pagerStrings[pagerStrings.length] = '\u5171<span style="color:#FF0000"> ' + this.recordCount + ' </span>\u6761\u8bb0\u5f55 ';
pagerStrings[pagerStrings.length] = '\u6bcf\u9875\u663e\u793a<span style="color:#FF0000"> ';
pagerStrings[pagerStrings.length] = '<select id="selectRowCount" onchange="pager.setRowCount()"><option value="10" ';
if (this.pageSize == 10)
{
pagerStrings[pagerStrings.length] = 'selected';
}
pagerStrings[pagerStrings.length] ='>10</option><option value="20" ';
if (this.pageSize == 20)
{
pagerStrings[pagerStrings.length] = 'selected';
}
pagerStrings[pagerStrings.length] ='>20</option><option value="50" ';
if (this.pageSize == 50)
{
pagerStrings[pagerStrings.length] = 'selected';
}
pagerStrings[pagerStrings.length] ='>50</option><option value="100" ';
if (this.pageSize == 100)
{
pagerStrings[pagerStrings.length] = 'selected';
}
pagerStrings[pagerStrings.length] = '>100</option></select>';
pagerStrings[pagerStrings.length] = ' </span>\u6761 \u5206<span style="color:#FF0000"> ' + pageCount + ' </span>\u9875 ';
if (this.currentPage == 1 || pageCount <= 1) {
pagerStrings[pagerStrings.length] = '<span style="color:#CCCCCC;"> \u9996\u9875 \u4e0a\u4e00\u9875 </span>';
} else {
pagerStrings[pagerStrings.length] = '<span style="cursor:pointer;text-decoration: underline;" onclick="pager.gotoPage(1);">\u9996\u9875</span> <span style="cursor:pointer;text-decoration: underline;" onclick="pager.gotoPage('
+ previousPage + ');">\u4e0a\u4e00\u9875</span> ';
}
if (this.currentPage == pageCount || pageCount <= 1) {
pagerStrings[pagerStrings.length] = '<span style="color:#CCCCCC;"> \u4e0b\u4e00\u9875 \u5c3e\u9875</span>';
} else {
pagerStrings[pagerStrings.length] = '<span style="cursor:pointer;text-decoration: underline;" onclick="pager.gotoPage(' + nextPage + ');">\u4e0b\u4e00\u9875</span> <span style="cursor:pointer;text-decoration: underline;"
onclick="pager.gotoPage(' + lastPage + ');">\u5c3e\u9875</span> ';
}
pagerStrings[pagerStrings.length] = ' \u7b2c <input style="text-align:center;border: 1px solid #CCCCCC;" name="pager.requestPage" type="text" id="reqPage" size="4" maxlength="6" value="' + this.currentPage + '" onchange="this.value
= isInteger(this.value) ? this.value : 1" /> \u9875 ';
pagerStrings[pagerStrings.length] = '<input name="buttonsubmitPage" type="button" id="buttonsubmitPage" value="\u8f6c\u5230" onclick="pager.gotoPage($(\'#reqPage\').attr(\'value\'));" />';
return pagerStrings.join("").toString();
};
function isInteger(value) {
return /^[-\+]?\d+$/.test(value)
};
function showPaginIcon(form,recordCount,pageSize,currentPage){
var pager = new Pager(form,recordCount,pageSize,currentPage);
document.write(pager.toString());
}
相关文章推荐
- 目前最好的JSP分页技术
- JSP分页技术实现 使用工具类实现通用分页处理
- jsp分页技术的例子实现和总结
- 一个非常简单的分页技术MYSQL+JSP 利用了mysql的LIMIT参数
- JSP分页技术
- JSP分页技术实现
- JSP分页技术实现
- jsp读取数据库实现分页技术简析
- JSP分页技术实现[转]
- 一个非常简单的分页技术MYSQL+JSP 利用了mysql的LIMIT参数
- JSP分页技术小结
- 目前最好的JSP分页技术
- Java web分页技术(jsp+servlet+Dao+javabean)
- [置顶] 目前最好的JSP分页技术
- JSP分页技术实现
- jsp分页技术
- JSP分页技术实现
- 目前最好的JSP分页技术
- 纯Jsp分页技术
- 目前最好的JSP分页技术