转自:http://blog.youkuaiyun.com/wallimn/archive/2008/04/05/2252873.aspx
JSP中,使用JSTL分页,效率高与不高我们姑且不论,还是相对简单一点。网上相应的文章也很多,但是很少有比较好的处理地址栏参数(也就是查询参数)的方案。有人使用session来保存查询参数,应该算不上是个好办法。
本文介绍一种方法,使用javascript分析地址栏参数,使用正则表达式处理页码指示数字的变化。可以达到一种简单而能用的效果。
下面是一个示例页面,因为没有数据库,它并不能运行,感兴趣的人,请花点时间看一下代码,很简单,并有适量的注释,相信应该不难理解。
<%@ page import="java.util.Date" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//--==分页变量初始化==--
Integer rowspp = Integer.valueOf(11); //每页行数
request.setAttribute("rowspp",rowspp);//每页行数
String pageindex = request.getParameter("page");
//System.out.println(pageindex);
//当前页码,默认为0
if(pageindex==null)pageindex="0";
request.setAttribute("pageindex",pageindex);//在页面里使用这个变量,防止用户没有传递个为值出错
//--==分页变量初始化==--
String selectSql = "select * from GSP where 1=1 ";
String countSql = " select count(*) as c from GSP where 1=1 ";
//拼接查询条件
String where = "";
String value = request.getParameter("name");
if(value!=null && !"".equals(value)){
where+=" and name like '%"+value+"%' ";
}
value = request.getParameter("county");
if(value!=null && !"".equals(value)){
where+=" and county like '%"+value+"%' ";
}
value = request.getParameter("scope");
if(value!=null && !"".equals(value)){
where+=" and scope like '%"+value+"%' ";
}
request.setAttribute("selectSql",selectSql+where);
request.setAttribute("countSql",countSql+where);
%>
<sql:query var="countresults" sql = "${countSql}"/>
<c:set value="${countresults.rows[0].c}" var="rowcount"/>
<c:set value="${rowcount % rowspp==0?(rowcount/rowspp+1):(rowcount/rowspp)}" var="pagecount"/>
<sql:query var="results" sql="${selectSql}" startRow="${rowspp*pageindex}" maxRows="${rowspp}"></sql:query>
<%--功能:通过jstl实现数据带条件查询,结果分页显示--%>
<%--特点:比较简单,分页机制可以通用--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>"/>
<title>单位查询结果显示页面</title>
<script type="text/javascript">...
function pagechange(index)...{
var url = document.location;//原始的地址
//使用正则达式,假定当前页参数在最后一个,且形如page=4
var regExp = /(S*page=)(S*)/;
var arr = regExp.exec(url);//使用正则表达式分析页面的地址,使用分析结果调整页码
if(arr==null)url = url+"&page="+index;
else url = arr[1]+index;
//alert(url);
window.location = url;
}
</script>
</head>
<body>
<center>总记录数:${rowcount};页数:${pagecount},当前页:${pageindex+1}
<a href="javascript:pagechange(0)" title="首页"><<</a>
<c:if test="${pageindex > 0}">
<a href="javascript:pagechange(${pageindex-1})" title="前一页"><</a>
</c:if>
<c:if test="${(pageindex+0) < (pagecount-1)}">
<a href="javascript:pagechange(${pageindex+1})" title="下一页">></a>
</c:if>
<a href="javascript:pagechange(${pagecount-1})" title="尾页">>></a></center>
<table border="1" width="86%" align="center" class="t1"><%...--统一控制数据展现表格的样式--%>
<tr>
<th width="30%">企业名称</th>
<th width="35%">通信地址</th>
<th width="10%">证书编号</th>
<th width="10%">有效期</th>
<th width="15%">操作</th>
</tr>
<c:forEach var="row" items="${results.rows}" varStatus="s">
<tr class="tr${s.index % 2}"><%...--通过样式控制奇数行与偶数行背景不同--%>
<td>${row.name}</td>
<td>${row.address}</td>
<td>${row.cerno}</td>
<td><fmt:formatDate value="${row.vtm}" pattern="yyyy-MM-dd"/></td>
<td align="center">查看删除修改操作</td>
</tr>
</c:forEach>
</table>
</body>
</html>