Struts+Hibernate开发实践 分页的实现[3]

[入库:2005年8月19日] [更新:2007年3月25日]

本文简介:选择自 chendreamer 的 blog

                                     httpservletresponse httpservletresponse) {

    collection clinfos = null;//用于输出到页面的记录集合

    int totalrows;//记录总行数

    vehiclepropertydao vehicledao = new vehiclepropertydao();

    //取得当前表中的总行数
    try {
      totalrows = vehicledao.getrows("select count(*) from vehicleproperty");
    } catch (exception ex) {
      servlet.log(ex.tostring());
      return actionmapping.findforward(constants.failure);
    }

    //通过pagerhelper类来获取用于输出到页面的pager对象
    pager pager=pagerhelper.getpager(httpservletrequest,totalrows);

    //取出从startrow开始的pagesize行记录
    try {
      clinfos = vehicledao.findwithpage(pager.getpagesize(), pager.getstartrow());
    } catch (exception ex) {
      servlet.log(ex.tostring());
      return actionmapping.findforward(constants.failure);
    }

    //把输出的记录集和pager对象保存到request对象中
    httpservletrequest.setattribute("clinfos", clinfos);
    httpservletrequest.setattribute("pager", pager);

    return actionmapping.findforward(constants.success);
  }


查询语句select count(*) from vehicleproperty 也可以换成你需要的任意的条件(select count(*) from vehicleproperty where ..)


5.jsp页面使用

下面就是在jsp中的应用了:

<td colspan="8" align="right" class="head">
   第<bean:write name="pager" property="currentpage"/>页&nbsp;
   共<bean:write name="pager" property="totalpages"/>页&nbsp;
   <html:link action="/bussiness/clinfo/querywithpage.do?method=querywithpage&amp;pagemethod=first" paramname="pager" paramproperty="currentpage" paramid="currentpage">首页</html:link>
   <html:link action="/bussiness/clinfo/querywithpage.do?method=querywithpage&amp;pagemethod=previous" paramname="pager" paramproperty="currentpage" paramid="currentpage">上一页</html:link>
   <html:link action="/bussiness/clinfo/querywithpage.do?method=querywithpage&amp;pagemethod=next" paramname="pager" paramproperty="currentpage" paramid="currentpage">下一页</html:link>
   <html:link action="/bussiness/clinfo/querywithpage.do?method=querywithpage&amp;pagemethod=last" paramname="pager" paramproperty="currentpage" paramid="currentpage">尾页</html:link>
</td>


解释一下这一行:"/bussiness/clinfo/querywithpage.do?method=querywithpage&amp;pagemethod=first
method=querywithpage 是由于我的action继承的是dispatchaction,需要一个method参数
pagemethod=first
是用来在pagehelper类中判断执行哪个操作

四、总结

我做的这个也只是一个借鉴,还有很多没有实现的,比如还可以加一下 go 直接到第n页的功能。

本文关键:Struts+Hibernate开发实践 分页的实现
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top