Hibernate+Struts模糊查詢+分頁

分頁一般需要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>
	&nbsp;&nbsp;&nbsp;&nbsp;
	<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>
	&nbsp;&nbsp;&nbsp;&nbsp;
	<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>
	&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="javascript:gotoPage(${totalPage},'${keyName}','${byType}');">末頁</a>
	&nbsp;&nbsp;&nbsp;&nbsp;
	第${pageNum}頁/共${totalPage}頁
	&nbsp;&nbsp;&nbsp;&nbsp;
	跳轉到第
	<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");

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章