struts2实现分页技术:
在对MemberUser类(表)中的数据进行分页:
1.在MemberUserDaoImpl 中添加 获取数据库中表的总数量的方法:
public int counterByPage() { // TODO Auto-generated method stub sql="select count(*) from memberuser"; ResultSet rs =util.Query(sql); int totalCount=0; try { while(rs.next()) { totalCount = rs.getInt(1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return totalCount; }
2.在MemberUserDaoImpl中添加 获取当前页的数据集合的方法:
public ListqueryMemberUserInfoByPage(int pageNow,int limitPage) { // TODO Auto-generated method stub sql="SELECT * FROM (SELECT A.*, ROWNUM RN FROM (select M.MEMBERID,M.MEMBERNAME,M.AGE,M.GENDER,M.BIRTHDAY,M.MEMBER_LABEL,M.MEMBER_INTRODUCTION,M.MEMBER_PICTURE,M.MEMBER_INTEGRAL,M.REGISTER_DATE,P.ROLENAME,M.ACCOUNTNUMBER,M.PWD,M.MEMBEREMAIL from memberuser m,person_role p where m.roleid=p.roleid order by m.memberid Asc) A WHERE ROWNUM <=?)WHERE RN >?"; List memberList = new ArrayList (); ResultSet rs = util.Query(sql,limitPage*pageNow,(pageNow-1)*limitPage); try { while(rs.next()) { MemberUserRole mUserRole = new MemberUserRole(rs.getInt("MEMBERID"),rs.getString("memberName"),rs.getInt("age"),rs.getString("gender"),rs.getDate("birthday"),rs.getString("MEMBER_LABEL"),rs.getString("MEMBER_INTRODUCTION"),rs.getString("MEMBER_PICTURE"),rs.getDate("REGISTER_DATE"),rs.getString("roleName"),rs.getString("pwd"),rs.getInt("MEMBER_INTEGRAL"),rs.getString("ACCOUNTNUMBER"),rs.getString("MEMBEREMAIL")); memberList.add(mUserRole); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { util.close(); } return memberList; }
在Acction中:
public class PageAction extends BaseAction { /** * */ private static final long serialVersionUID = 1L; /** * 总记录数 */ private int totalRecord; /** * 每页显示条数 */ private int limitPage=5; /** * 当前页 */ private int nowPage=1; /** * 总页数 */ private int totalPage; public int getTotalRecord() { return totalRecord; } public void setTotalRecord(int totalRecord) { this.totalRecord = totalRecord; } public int getLimitPage() { return limitPage; } public void setLimitPage(int limitPage) { this.limitPage = limitPage; } public int getNowPage() { return nowPage; } public void setNowPage(int nowPage) { this.nowPage = nowPage; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } MemberUserBiz memberUserBiz = new MemberUserBizImpl(); /** * @return * 获取某页用户信息的记录 */ public String queryMemberUserInfoByPage() { ListmemberListByPage = new ArrayList (); //获取总记录数 totalRecord= memberUserBiz.counterByPage(); //获取总页数 totalPage =(totalRecord+limitPage-1)/limitPage; //当前页处理 if(nowPage<1) { nowPage=1; }else if(nowPage>=totalPage) { nowPage=totalPage; } //获得某页的记录 memberListByPage = memberUserBiz.queryMemberUserInfoByPage(nowPage, limitPage); app.setAttribute("memberListByPage", memberListByPage); int a=0; req.setAttribute("a", a); return "queryMemberUserInfoByPage"; }}
在jsp页面:
以上就是比较完整的代码了
版权声明:本文为博主原创文章,未经博主允许不得转载。