分页一般需要pageNum,pageSize,totalPage,totalRow这几个参数。
我写的是按照名称和类型进行关键字查询。
DAO层代码:
public static int findTotalRow(String keyName,String byType){
int count=0;
Session se=HibernateUtil.getSession();
String hql="from Course c where 1=1";
if(keyName!=null&&!keyName.equals("")){
hql+="and c.name like '%"+keyName+"%'";
}
if(byType!=null&&!byType.equals("")&&!byType.equals("全部类型")){
hql+="and c.type='"+byType+"'";
}
count=se.createQuery(hql).list().size();
HibernateUtil.closeSession(se);
return count;
}
public static List findAll(String keyName,String byType,int pageNum,int pageSize){
List list=null;
Session se=HibernateUtil.getSession();
String hql="from Course c where 1=1";
if(keyName!=null&&!keyName.equals("")){
hql+="and c.name like '%"+keyName+"%'";
}
if(byType!=null&&!byType.equals("")&&!byType.equals("全部类型")){
hql+="and c.type='"+byType+"'";
}
list=se.createQuery(hql)
.setFirstResult((pageNum-1)*pageSize)
.setMaxResults(pageSize)
.list();
HibernateUtil.closeSession(se);
return list;
}
Action的代码:
public String tolist() throws Exception {
keyName=URLDecoder.decode(keyName, "utf-8");
byType=URLDecoder.decode(byType, "utf-8");
totalRow=CourseDao.findTotalRow(keyName,byType);
courses=CourseDao.findAll(keyName,byType,pageNum,pageSize);
totalPage=totalRow/pageSize;
if(totalRow%pageSize!=0){
totalPage=totalPage+1;
}
return "courseList.jsp";
}
jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
});
function mydel(id){
var flag=confirm("您确定删除该条信息吗?");
if(flag){
location='courseAction_del?id='+id;
}
}
function gotoPage(pageNum,keyName,byType){
keyName=encodeURI(encodeURI(keyName));
byType=encodeURI(encodeURI(byType));
//var n=document.getElementById("mysel").value;
location="courseAction_tolist?pageNum="+pageNum+"&keyName="+keyName+"&byType="+byType;
}
function gotoPage2(keyName,byType){
keyName=encodeURI(encodeURI(keyName));
byType=encodeURI(encodeURI(byType));
var n=document.getElementById("mysel").value;
location="courseAction_tolist?pageNum="+n+"&keyName="+keyName+"&byType="+byType;
}
</script>
</head>
<h3>课程列表</h3>
<form action="courseAction_tolist" method="post">
名称:<input type="text" name="keyName" value="${keyName}" />
类型:<select name="byType">
<option value="全部类型" ${byType=="全部类型"?"selected='selected'":""}>全部类型</option>
<option value="java类" ${byType=="java类"?"selected='selected'":""}>java类</option>
<option value="基础类" ${byType=="基础类"?"selected='selected'":""}>基础类</option>
<option value="数据库类" ${byType=="数据库类"?"selected='selected'":""}>数据库类</option>
<option value="网页类" ${byType=="网页类"?"selected='selected'":""}>网页类</option>
</select>
<input type="submit" value="查询"/>
</form>
<table border="1" width="700">
<tr>
<th>编号</th>
<th>名称</th>
<th>类型</th>
<th>内容</th>
<th>学分</th>
<th>操作</th>
</tr>
<c:forEach items="${courses}" var="c">
<tr>
<td>${c.cno}</td>
<td>${c.name}</td>
<td>${c.type}</td>
<td>${c.contents}</td>
<td>${c.point}</td>
<td><a href="courseAction_toupd?id=${c.id}">修改</a> | <a href="javascript:mydel(${c.id});">删除</a></td>
</tr>
</c:forEach>
</table>
<a href="javascript:gotoPage(1,'${keyName}','${byType}');">首页</a>
<c:choose>
<c:when test="${pageNum>1}">
<a href="javascript:gotoPage(${pageNum-1},'${keyName}','${byType}');">上一页</a>
</c:when>
<c:otherwise>
<a>上一页</a>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${pageNum<totalPage}">
<a href="javascript:gotoPage(${pageNum+1},'${keyName}','${byType}');">下一页</a>
</c:when>
<c:otherwise>
<a>下一页</a>
</c:otherwise>
</c:choose>
<a href="javascript:gotoPage(${totalPage},'${keyName}','${byType}');">末页</a>
第${pageNum}页/共${totalPage}页
跳转到第
<select id="mysel" onchange="gotoPage2('${keyName}','${byType}');">
<c:forEach begin="1" end="${totalPage}" step="1" var="p">
<option value="${p}" ${p==pageNum?"selected='selected'":""}>${p}</option>
</c:forEach>
</select>
页
</html>
注意点:利用get请求传参时中文会发生乱码,所以在jsp页面需要编码,后端需要解码:
keyName=encodeURI(encodeURI(keyName));
byType=encodeURI(encodeURI(byType));
-------------------------------------------------------------------------------------------------------
keyName=URLDecoder.decode(keyName, "utf-8");
byType=URLDecoder.decode(byType, "utf-8");