使用SSH框架实现分页功能

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

 

 

转载于:https://www.cnblogs.com/szq1047/p/4857696.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值