首页 > 其他 > 详细

使用SSH框架实现分页功能

时间:2015-10-06 21:59:36      阅读:635      评论:0      收藏:0      [点我收藏+]

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.最终页面展示:

 技术分享

 

使用SSH框架实现分页功能

原文:http://www.cnblogs.com/szq1047/p/4857696.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!