1.最近在整合SSH框架,將原先使用JSP+Servlet的一個房間管理系統,改為使用SSH框架來實現,這里主要講的是一個分頁的功能的實現:主要使用的數據的軟件是Mysql,實現分頁的主要的兩句語句就是:
Criteria criteria=session.createCriteria(RoomInfo.class);
criteria.setFirstResult(startIndex);//startIndex表示開始的記錄
criteria.setMaxResults(rows);//rows表示的是沒有的記錄條數
其實就是相當於mysql數據庫中的查詢語句:select*from room limit startIndex,rows;
2.首先是dao層的RoomDAO中定義兩個接口
public List search(RoomInfo roomInfo,int startIndex,int rows);//這個接口是用於獲取指定頁的房間記錄信息,roomInfo表示RoomInfo房間實體類,startIndex表示開始的記錄,rows表示每頁的記錄數
public int rowsCount(RoomInforoom Info);//這個接口是用於得到總的記錄條數,用於計算頁數
在RoomDAOImpl實現這兩個接口,具體代碼如下:
public List search(final RoomInfo roomInfo,final int startIndex,final int rows){
return template.executeFind(
new HibernateCallback(){
public Object doInHibernate(Sessionsession) throws HibernateException,SQLException{
Criteria criteria=session.createCriteria(RoomInfo.class);
criteria.setFirstResult(startIndex);
criteria.setMaxResults(rows);
//查出房間號大於101且小於201的房間信息
//criteria.add(Restrictions.conjunction().add(Restrictions.gt("Rno",101)).add(Restrictions.lt("Rno",201)));
if(null!=roomInfo){
criteria.add(Example.create(roomInfo));
}
returncriteria.list();
}
});
}
//計算總的行數
public int rowsCount(RoomInfo roomInfo){
String hql="select count(*)from RoomInfo";
int rows=Integer.parseInt(template.find(hql).get(0).toString());
return rows;
}
3.在Biz層主要是調用DAO層的信息,同樣的在RoomBiz接口中定義了兩個接口與RoomDAO對應
publicList<RoomInfo>getRoomInfo(RoomInforoomInfo,intstartIndex,introws);
publicintrowsCount(RoomInforoomInfo);
在RoomBizImpl實現這兩個接口
public List<RoomInfo> getRoomInfo(RoomInfo roomInfos,int startIndex,int rows){
RoomInfo roomInfo=null;
List<RoomInfo>list=roomDAO.search(roomInfos,startIndex,rows);
return list;
}
4.在RoomAction業務邏輯層中定義三個屬性,並得到其getter和setter方法,分別是:
privateint currentPage;//當前頁
privateint pageCount;//共有多少頁
privateint rows;//每頁多少行
在RoomAction中定義方法用於獲取當前頁的房間信息,並返回一個String字符串:
public String getRoomInfo(){
roomBiz=serviceManager.getRoomBiz();
ActionContext actionContext=ActionContext.getContext();
try{
int startIndex=(currentPage-1)*rows;//表示從頁面中獲取當前頁數,通過當前頁數計算出當前頁起始的記錄號
roomList=roomBiz.getRoomInfo(roomInfos,startIndex,rows);//得到指定頁的房間信息
pageCount=(roomBiz.rowsCount(roomInfos)-1)/rows+1;//這里是計算總共有多少頁,roomBiz.rowsCount(roomInfos)
//是得到總的記錄數,如果是15條記錄,每頁4條,那么應該是4頁,而不是3頁
//System.out.println("room========="+roomList.get(1).getRno());
if(null!=roomList){
actionContext.put("roomList",roomList);
actionContext.put("pageCount",pageCount);
//System.out.println("actionContext=========="+actionContext.get("roomList"));
}
}catch(Exceptione){
System.out.print("error=========="+e.getMessage());
}
if(roomList!=null){
return"roomManager";
}else{
return"error";
}
}
5.在struts.xml的配置
<!--為UserAction類的login方法配置映射-->
<action name="getListRoom"class="com.hibtest3.action.RoomAction" method="getRoomInfo">
<result name="roomManager">/roomManage.jsp</result>
<result type="redirect"name="error">/error.jsp</result>
<param name="rows">4</param>//這里需要設置每頁的記錄數
</action>
6.在roomManage.jsp頁面中的調用
List<RoomInfo> roomInfoList=(List<RoomInfo>)request.getAttribute("roomList");//返回到頁面中當前頁的記錄
int pagesCount=Integer.parseInt(request.getAttribute("pageCount").toString());//返回總的頁數
7.最終頁面展示: