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 List<MemberUserRole> queryMemberUserInfoByPage(int pageNow,int limitPage) { //pageNow表示当前页;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<MemberUserRole> memberList = new ArrayList<MemberUserRole>(); 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"),
<span style="white-space:pre"> </span>rs.getString("gender"),rs.getDate("birthday"),rs.getString("MEMBER_LABEL"),rs.getString("MEMBER_INTRODUCTION"),
<span style="white-space:pre"> </span>rs.getString("MEMBER_PICTURE"),rs.getDate("REGISTER_DATE"),rs.getString("roleName"),rs.getString("pwd"),rs.getInt("MEMBER_INTEGRAL"),
<span style="white-space:pre"> </span>rs.getString("ACCOUNTNUMBER"),<span style="font-family: Arial, Helvetica, sans-serif;">rs.getString("MEMBEREMAIL"));</span>
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() { List<MemberUserRole> memberListByPage = new ArrayList<MemberUserRole>(); //获取总记录数 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页面:
<div id="tableInfo" align="center"> <s:form name="userform" method="post"> <table border="1" id="userInfoTable"> <tr id="bt"> <th>序号</th> <th>账号</th> <th>用户名</th> <th>年龄</th> <th>性别</th> <th>生日</th> <th>积分</th> <th>角色</th> <th>邮箱</th> <s:if test="${mUser.roleId==1}"> <th>操作</th> </s:if> </tr> <s:iterator value="#application.memberListByPage" status="status" id="userInfo"> <tr id="info" bgcolor="${status.index%2 == 0?'#D0D8E8':'#E9EDF4'}"> <td>${status.index+1}</td> <td><s:property value="#userInfo.accountNumber" /></td> <td><s:property value="#userInfo.memberName" /></td> <td><s:property value="#userInfo.age" /></td> <td><s:property value="#userInfo.gender" /></td> <td><s:property value="#userInfo.birthday" /></td> <td><s:property value="#userInfo.memberIntegral" /></td> <td><s:property value="#userInfo.roleName" /></td> <td><s:property value="#userInfo.memberEmail" /></td> <s:if test="${mUser.roleId==1}"> <td><a href="member_adminCheckMemberById.action?memberId=${userInfo.memberId}">查看</a> | <a href="member_deleteMember.action?memberId=${userInfo.memberId}"><span>删除</span> </a>| <a href="member_adminQueryMemberById.action?memberId=${userInfo.memberId}">更新</a> </td> </s:if> </tr> </s:iterator> </table> <!-- 分页技术的实现--> <div> <center> 共<s:property value="totalRecord" />条记录 共 ${totalPage} 页 第<s:property value="nowPage" />页 <s:a href="page_queryMemberUserInfoByPage.action?nowPage=${1}">首页</s:a> <s:if test="nowPage>1"> <s:a href="page_queryMemberUserInfoByPage.action?nowPage=${nowPage-1}">上一页</s:a> </s:if> <s:if test="nowPage<totalPage"> <s:a href="page_queryMemberUserInfoByPage.action?nowPage=${nowPage+1}">下一页</s:a> </s:if> <s:a href="page_queryMemberUserInfoByPage.action?nowPage=${totalPage}">尾页</s:a> </center> </div> </s:form> </div>
以上就是比较完整的代码了
时间: 2024-10-31 07:07:47