SSI搜索分页

4 篇文章 0 订阅

目前还不会写分页标签,只用到了上一页、下一页、首页、末页。理清思路,在写分页的时候,其实需要的一般有总页数countPage、当前页面currentPage、总记录数countSize、单页记录数目pageSize就可以了。其中currentPage当前页面可以默认为1,单页记录数目pageSize根据需要赋值。

public class PageBean {
 private int pageSize;// 单页记录
 private int currentPage;// 当前页面
 private int countSize;// 总记录数
 private int countPage;// 页面数目
 private int pageStartId;// 当前页起始数据id

省略getter、setter

}

上面是page的bean,需要进行分页的bean继承下PageBean就可以了。同时,由于是SSI框架,每层的数据都要分开处理,所以对应的,也有个PageModel,里面内容和PageBean是一样的,需要分页的Model继承下就可以了。我使用的是ReserveBean和ReserveModel,分别继承PageBeanPageModel

 

写分页时最重要的参数一般是每页开始数pageStartId,每页结束数pageEndId,当前页面currentPage和单页记录pageSize,具体是什么要根据数据库来定,我用的是mysql,用的是pageStartId和pageSize,在sql语句里面写的是:

查询符合搜索条件的总记录数目

<select id="countSize" resultClass="java.lang.Integer"
  parameterClass="reserve">
  SELECT count(1) FROM reserve
  <dynamic prepend="where">
   <isNotEmpty property="bookDay" prepend="AND">
    bookday=#bookDay#
   </isNotEmpty>
   <isNotEmpty property="bookUserName" prepend="AND">
    cname like
    '%$bookUserName$%'
   </isNotEmpty>
   <isNotEmpty property="roomId" prepend="AND">
    roomid=#roomId#
   </isNotEmpty>
  </dynamic>
 </select>

 

查询符合条件的记录

<select id="queryAllReserveForPage" resultMap="get-all-reserve"
  parameterClass="reserve">
  SELECT  balabala  FROM balabala
  where 搜索条件
  order by S3.rid limit #pageStartId#,#pageSize#
 </select>

根据这两条sql语句,分别编写方法:

Dao层:接口里面有这2个方法

//查询数据中符合条件(预定日期、预订人、房间名称)的预定记录条数

public Integer queryCountSize(ReserveModel model) {
  Integer count=(Integer) getSqlMapClientTemplate().queryForObject("countSize", model);
  return count;
 }

//获取符合条件的预定记录

public List<ReserveModel> queryPageList(ReserveModel model) {
  List<ReserveModel> list=null;
  list=getSqlMapClientTemplate().queryForList("queryAllReserveForPage", model);
  return list;
 }

 

service层:

注意导入DAO,形如:

private BookRoomDao bookRoomDao;

 public void setBookRoomDao(BookRoomDao bookRoomDao) {
  this.bookRoomDao = bookRoomDao;
 }

 

//查询数据中符合条件(预定日期、预订人、房间名称)的预定记录条数

public Integer queryCountSize(ReserveBean bean) {
  ReserveModel model = new ReserveModel();
  model.setBookDay(bean.getBookDay());
  model.setRoomId(bean.getRoomId());
  model.setBookUserName(bean.getBookUserName());
  Integer count = 0;
  try {
   count = bookRoomDao.queryCountSize(model);
  } catch (Exception e) {
   return 0;
  }
  return count;
 }

 

//获取符合条件的预定记录

public List<ReserveBean> queryAllReserveForPage(ReserveBean bean) {
  ReserveModel model = new ReserveModel();
  RefUtil.convertBean2Bean(bean, model);//这是复制bean里面的值到model的方法,也可以用上面的方法里面的get方法来得到同样的效果
  model.setCurrentPage(bean.getCurrentPage());
  model.setPageSize(bean.getPageSize());
  model.setPageStartId((bean.getCurrentPage() - 1) * (bean.getPageSize()));//在service层计算pageStartId的值
  List<ReserveBean> blist = new ArrayList<ReserveBean>();
  List<ReserveModel> modelList = bookRoomDao.queryPageList(model);
  for (ReserveModel m : modelList) {
   ReserveBean b = new ReserveBean();
   RefUtil.convertBean2Bean(m, b);
   blist.add(b);
  }
  return blist;
 }

 

在action层里面:

注意导入service,形如:

private BookRoomService bookRoomService;
public void setBookRoomService(BookRoomService bookRoomService) {
  this.bookRoomService = bookRoomService;
 }

 

private int currentPage=1;// 当前页面
private int pageSize=10;//单页记录数目

public String queryList() {
  ReserveBean rBean = new ReserveBean();
  rBean.setBookDay(bookDay);
  rBean.setBookUserName(bookUserName);
  rBean.setRoomId(roomId);
  rBean.setCurrentPage(currentPage);//这两行比较重要,是分页中需要用到的,上面3行根据自己的程序改
  rBean.setPageSize(pageSize);
  
  countSize=bookRoomService.queryCountSize(rBean);
  countPage=page.getCountPage(pageSize, countSize);//我自己写了个方法得到总页数
  reserveList = bookRoomService.queryAllReserveForPage(rBean);
  return SUCCESS;
 }

 

//获取总页数

public int getCountPage(int pageSize, int countSize) {
  int i = countSize / pageSize;
  if (countSize % pageSize != 0) {
   i += 1;
  }
  return i;
 }

 

在jsp里面,可以有2种方法,推荐第二种:

1、在查询的form里面,添加超链接,但是这种有个问题,如果搜索条件过多的话会很累赘。

<div>
     第
     <s:property value="currentPage" />
     页/共
     <s:property value="countPage" />
     页&nbsp;
     <a
      href="/bookRoom/com/tc/bookRoom/book/book!queryList.action?currentPage=1
       &bookUserName=<s:property value="bookUserName"/>&bookDay=<s:property value="bookDay"/>&roomId=<s:property value="roomId"/>">首页</a>
     &nbsp;
     <s:if test="currentPage==1">上一页</s:if>
     <s:elseif test="currentPage!=1">
      <a
       href="/bookRoom/com/tc/bookRoom/book/book!queryList.action?currentPage=<s:property value="(currentPage-1)" />
       &bookUserName=<s:property value="bookUserName"/>&bookDay=<s:property value="bookDay"/>&roomId=<s:property value="roomId"/>">上一页</a>
     </s:elseif>
     &nbsp;|
     <s:if test="currentPage==countPage">下一页</s:if>
     <s:elseif test="currentPage!=countPage">
      <a
       href="/bookRoom/com/tc/bookRoom/book/book!queryList.action?currentPage=<s:property value="(currentPage+1)" />
       &bookUserName=<s:property value="bookUserName"/>&bookDay=<s:property value="bookDay"/>&roomId=<s:property value="roomId"/>">下一页</a>
     </s:elseif>
     <a
      href="/bookRoom/com/tc/bookRoom/book/book!queryList.action?currentPage=<s:property value="countPage" />
       &bookUserName=<s:property value="bookUserName"/>&bookDay=<s:property value="bookDay"/>&roomId=<s:property value="roomId"/>">末页</a>
    </div>

2、使用form表单提交,结合JS:

这种可以将搜索条件保存起来,直接可以用。这段div也是包含在form表单里面的。

注意form表单是<s:form/>形式的,在头文件里面需要引入<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>(我这种写法是html文件里面的,如果是Xhtml请自己百度)

<div>
     第
     <s:property value="currentPage"/>
     <input type="hidden" value="<s:property value='currentPage'/>" id="currentPage"/>
     页/共
     <s:property value="countPage" />
     <input type="hidden" value="<s:property value='countPage'/>" id="countPage"/>
     页&nbsp;
     <a id="homePage">首页</a> &nbsp;
     <s:if test="currentPage==1">上一页</s:if>
     <s:elseif test="currentPage!=1">
      <a id="backPage">上一页</a>
     </s:elseif>
     &nbsp;|
     <s:if test="currentPage==countPage">下一页</s:if>
     <s:elseif test="currentPage!=countPage">
      <a id="nextPage">下一页</a>
     </s:elseif>
     &nbsp;
     <a id="lastPage">末页</a>
    </div>

 

js代码:

别忘了在head部分引入:<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>

//分页查询“首页”
$(function() {
    $("#homePage").click(function() {
        document.mqueryForm.action = "/bookRoom/com/tc/bookRoom/book/book!queryList.action?currentPage=1";
        document.mqueryForm.submit();
        return true;
    });
});
//分页查询“上一页”
$(function() {
    $("#backPage").click(function() {
        var currentPage = parseInt($("#currentPage").val()) - 1;
        document.mqueryForm.action = "/bookRoom/com/tc/bookRoom/book/book!queryList.action?currentPage=" + currentPage;
        document.mqueryForm.submit();
        return true;
    });
});
//分页查询“下一页”
$(function() {
    $("#nextPage").click(function() {
        var currentPage = parseInt($("#currentPage").val()) + 1;
        document.mqueryForm.action = "/bookRoom/com/tc/bookRoom/book/book!queryList.action?currentPage=" + currentPage;
        document.mqueryForm.submit();
        return true;
    });
});
//分页查询“末页”
$(function() {
    $("#lastPage").click(function() {
        var currentPage = parseInt($("#countPage").val());
        document.mqueryForm.action = "/bookRoom/com/tc/bookRoom/book/book!queryList.action?currentPage=" + currentPage;
        document.mqueryForm.submit();
        return true;
    });
});

 

这样就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值