今天看了文章说可以用jquery的pagination的插件和struts2可以做出很好的分页效果.结果搞了半天一直提示pagination方法不能用.小弟不才从jquery1.4到juquery1.7都不行,当然pagination也换了几次.怒了,果断不要了.直接用最简单的.下面是我的一些代码.思想是这样:分两次查询数据,一次是根据条件查处总数.第二次是根据你的参数pageNo来查询每页的数据.在前台用一个简单的js技术实现他的点击事件..
这个图片都放到action里面了...
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><s:property value="getText('label.searchUser.title')" /></title> <jsp:include page="../hkgBanner.jsp"/> <script type="text/javascript"> function Countnext(pageNo) { document.getElementById("maintActionType").value = "Search"; document.getElementById("pageNo").value = pageNo; document.getElementById("searchUserForm").submit(); } </script> <s:form action="searchUserAccount.action" id="searchUserForm" method="post"> <s:hidden id="action_type" name="action_type"/> <s:hidden id="pageNo" name="pageNo"/> <table> <tr > <td>共<s:property value="rowCount"/>条记录 </td> <td>共<s:property value="pageCount"/>页 </td> <td><a href="javascript:Countnext('<s:property value="1"/>');">最前一页</a></td> <s:if test="%{1.equals(pageNo)}"> <td ><a href="javascript:Countnext('<s:property value="1"/>');">上一页</a></td> </s:if> <s:if test="%{(pageNo)>1}"> <td ><a href="javascript:Countnext('<s:property value="pageNo-1"/>');">上一页</a></td> </s:if> <s:if test="%{(pageNo)<(pageCount)}"> <td><a href="javascript:Countnext('<s:property value="pageNo+1"/>');">下一页</a></td> </s:if> <s:if test="%{(pageCount).equals(pageNo)}"> <td><a href="javascript:Countnext('<s:property value="pageCount"/>');">下一页</a></td> </s:if> <td><a href="javascript:Countnext('<s:property value="pageCount"/>');">最后一页</a></td> </tr> </table> </s:if> </s:form>
action
public class SearchUserAction extends BaseAction { private String action_type; private int rowCount=0;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class SearchUserBo extends BaseBo { 2 3 public List<LoginUserVo> search(LoginUserVo vo) throws SQLException { 4 List<LoginUserVo> list = null; 5 6 try { 7 this.start(); 8 LoginUserDao dao = new LoginUserDao(this.session); 9 list = dao.getLoginUser(vo); 10 this.commit(); 11 } finally { 12 this.close(); 13 } 14 15 return list; 16 } 17 18 public int searchall(LoginUserVo vo) throws SQLException { 19 int results=0; 20 21 try { 22 this.start(); 23 LoginUserDao dao = new LoginUserDao(this.session); 24 results = dao.getallLoginUser(vo); 25 this.commit(); 26 } finally { 27 this.close(); 28 } 29 30 return results; 31 } 32 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class LoginUserDao extends BaseDao { public LoginUserDao() { super(); } public LoginUserDao(SqlSession session) { super(session); } public List<LoginUserVo> getLoginUser(LoginUserVo vo) { return this.session.selectList("LoginUser.getLoginUser", vo); } public int getallLoginUser(LoginUserVo vo) { return this.session.selectOne("LoginUser.getallLoginUser", vo); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <select id="getLoginUser" parameterType="lcsd.hkg.vo.LoginUserVo" resultMap="loginUserMap"> 2 SELECT * FROM ( 3 SELECT U.USER_ID, 4 U.USER_NAME, 5 U.USER_NAME_CHI, 6 U.EMAIL, 7 G.ROLE_GROUP_CODE, 8 CASE WHEN U.ACTIVE='Y' THEN 'ACTIVE' ELSE 'INACTIVE' END AS ACTIVE, 9 U.USER_TITLE, 10 COUNT(*) OVER (PARTITION BY NULL) AS TOTAL_RECORDS, 11 ROW_NUMBER() OVER (PARTITION BY NULL ORDER BY NULL) AS ROWNUMBER, 12 U.LOGON_FAIL_COUNT, 13 CASE WHEN LOGON_FAIL_COUNT>=5 THEN 'Yes' ELSE 'No' END AS STR_LOGON 14 FROM LOGIN_USER U 15 INNER JOIN USER_ROLE R ON U.USER_ID = R.USER_ID 16 INNER JOIN ROLE_GROUP G ON R.ROLE_GROUP_CODE = G.ROLE_GROUP_CODE 17 <trim prefix="WHERE" prefixOverrides="AND | OR"> 18 <if test="userName != null and userName != ''"> 19 UPPER(U.USER_NAME) LIKE UPPER('%' || #{userName, jdbcType=VARCHAR} || '%') 20 </if> 21 <if test="userNameChi != null and userNameChi != ''"> 22 AND U.USER_NAME_CHI LIKE '%' || #{userNameChi, jdbcType=NVARCHAR} || '%' 23 </if> 24 <if test="email != null and email != ''"> 25 AND UPPER(U.EMAIL) LIKE UPPER('%' || #{email, jdbcType=VARCHAR} || '%') 26 </if> 27 <if test="userId != null and userId != ''"> 28 AND UPPER(U.User_ID) LIKE UPPER('%' || #{userId, jdbcType=VARCHAR} || '%') 29 </if> 30 <if test="roleGroupCode != null and roleGroupCode != ''"> 31 AND R.ROLE_GROUP_CODE = #{roleGroupCode, jdbcType=VARCHAR} 32 </if> 33 </trim> 34 35 ) TEMP 36 WHERE ROWNUMBER BETWEEN (#{pageNo, jdbcType=INTEGER}-1)*10 AND #{pageNo, jdbcType=INTEGER}*10 37 38 </select> 39 40 <select id="getallLoginUser" parameterType="lcsd.hkg.vo.LoginUserVo" resultType="java.lang.Integer"> 41 SELECT count(*) FROM ( 42 SELECT U.USER_ID, 43 U.USER_NAME, 44 U.USER_NAME_CHI, 45 U.EMAIL, 46 G.ROLE_GROUP_CODE, 47 CASE WHEN U.ACTIVE='Y' THEN 'ACTIVE' ELSE 'INACTIVE' END AS ACTIVE, 48 U.USER_TITLE, 49 COUNT(*) OVER (PARTITION BY NULL) AS TOTAL_RECORDS, 50 ROW_NUMBER() OVER (PARTITION BY NULL ORDER BY NULL) AS ROWNUMBER, 51 U.LOGON_FAIL_COUNT, 52 CASE WHEN LOGON_FAIL_COUNT>=5 THEN 'Yes' ELSE 'No' END AS STR_LOGON 53 FROM LOGIN_USER U 54 INNER JOIN USER_ROLE R ON U.USER_ID = R.USER_ID 55 INNER JOIN ROLE_GROUP G ON R.ROLE_GROUP_CODE = G.ROLE_GROUP_CODE 56 <trim prefix="WHERE" prefixOverrides="AND | OR"> 57 <if test="userName != null and userName != ''"> 58 UPPER(U.USER_NAME) LIKE UPPER('%' || #{userName, jdbcType=VARCHAR} || '%') 59 </if> 60 <if test="userNameChi != null and userNameChi != ''"> 61 AND U.USER_NAME_CHI LIKE '%' || #{userNameChi, jdbcType=NVARCHAR} || '%' 62 </if> 63 <if test="email != null and email != ''"> 64 AND UPPER(U.EMAIL) LIKE UPPER('%' || #{email, jdbcType=VARCHAR} || '%') 65 </if> 66 <if test="userId != null and userId != ''"> 67 AND UPPER(U.User_ID) LIKE UPPER('%' || #{userId, jdbcType=VARCHAR} || '%') 68 </if> 69 <if test="roleGroupCode != null and roleGroupCode != ''"> 70 AND R.ROLE_GROUP_CODE = #{roleGroupCode, jdbcType=VARCHAR} 71 </if> 72 </trim> 73 ) 74 </select>
接着上面的action
private int pageNo=1; private List<LoginUserVo> userList; private int pageCount=0; public List<LoginUserVo> getUserList() { return userList; } public void setUserList(List<LoginUserVo> userList) { this.userList = userList; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getRowCount() { return rowCount; } public void setRowCount(int rowCount) { this.rowCount = rowCount; } public String execute() throws Exception { if (action_type == null || action_type.isEmpty()) { this.doDefau(); } else if("Search".equals(action_type)) { this.doSearch(); } return SUCCESS; } private String doDefau() { return SUCCESS; } private String doSearch() { LoginUserVo vo = new LoginUserVo(); vo.setPageNo(pageNo); try { SearchUserBo bo = new SearchUserBo(); List<LoginUserVo> list = bo.search(vo); rowCount = bo.searchall(vo); pageCount =(rowCount/10); vo.setPageCount(pageCount); vo.setFullList(list); userList = list; request.setAttribute("rowCount", vo.getRowCount()); } catch (IllegalAccessError e) { logger.error(e); //add(ActionMessage.GLOBAL_MESSAGE); this.addFieldError("error", getText("global.Err.002",new String[]{e.getMessage()})); } catch (IllegalArgumentException e) { logger.error(e); this.addFieldError("error", getText("global.Err.002",new String[]{e.getMessage()})); } catch (SQLException e) { logger.error(e); this.addFieldError("error", getText("global.Err.002",new String[]{e.getMessage()})); } finally { if (!getActionErrors().isEmpty()) { this.addFieldError("error", getText("global.Err.002")); //this.setFieldErrors(getFieldErrors()); //saveErrors(pRequest, lvErrors); return SUCCESS; } } return SUCCESS; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class LoginUserVo extends BaseVo{ 2 private int pageNo= 1; 3 private int rowCount=0; 4 private int pageCount=0; 5 6 public int getPageNo() { 7 return pageNo; 8 } 9 public void setPageNo(int pageNo) { 10 this.pageNo = pageNo; 11 } 12 public int getRowCount() { 13 return rowCount; 14 } 15 public void setRowCount(int rowCount) { 16 this.rowCount = rowCount; 17 } 18 public int getPageCount() { 19 return pageCount; 20 } 21 public void setPageCount(int pageCount) { 22 this.pageCount = pageCount; 23 } 24 } 25
以上所继承的basevo和baseaction是自己扩展的东东希望没影响,其实你只要继承struts2就行了.还有就是sql用的是mybatis所以有点别扭.
嘿嘿 希望没有误导别人.基本的就这样.其实有很大的缺陷.还需要自己测试修改.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 7 <package name="default" namespace="/" extends="struts-default"> 8 <action name="searchUserAccount" class="com.yna.action.SearchUserAction" > 9 <result name="success">/sysadmin/searchUserAccount.jsp</result> 10 </action> 11 12 </package> 13 14 </struts>