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"/>页
共<bean:write name="pager" property="totalpages"/>页
<html:link action="/bussiness/clinfo/querywithpage.do?method=querywithpage&pagemethod=first" paramname="pager" paramproperty="currentpage" paramid="currentpage">首页</html:link>
<html:link action="/bussiness/clinfo/querywithpage.do?method=querywithpage&pagemethod=previous" paramname="pager" paramproperty="currentpage" paramid="currentpage">上一页</html:link>
<html:link action="/bussiness/clinfo/querywithpage.do?method=querywithpage&pagemethod=next" paramname="pager" paramproperty="currentpage" paramid="currentpage">下一页</html:link>
<html:link action="/bussiness/clinfo/querywithpage.do?method=querywithpage&pagemethod=last" paramname="pager" paramproperty="currentpage" paramid="currentpage">尾页</html:link>
</td>
解释一下这一行:"/bussiness/clinfo/querywithpage.do?method=querywithpage&pagemethod=first
method=querywithpage 是由于我的action继承的是dispatchaction,需要一个method参数
pagemethod=first 是用来在pagehelper类中判断执行哪个操作
四、总结
我做的这个也只是一个借鉴,还有很多没有实现的,比如还可以加一下 go 直接到第n页的功能。