//分页对象 protected PageResult pageResult; //页号 private int pageNo; //页大小 private int pageSize;c
package cn.edu.hpu.tax.core.action; import cn.edu.hpu.tax.core.page.PageResult; import com.opensymphony.xwork2.ActionSupport; public abstract class BaseAction extends ActionSupport{ protected String[] selectedRow; //分页对象 protected PageResult pageResult; //页号 private int pageNo; //页大小 private int pageSize; //默认每页页大小 public static int DEFAULT_PAGE_SIZE=4; public String[] getSelectedRow() { return selectedRow; } public void setSelectedRow(String[] selectedRow) { this.selectedRow = selectedRow; } public PageResult getPageResult() { return pageResult; } public void setPageResult(PageResult pageResult) { this.pageResult = pageResult; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getPageSize() { //给的默认的分页大小 if(pageSize < 1) pageSize = DEFAULT_PAGE_SIZE; return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
//列表页面 public String listUI() throws Exception{ try { QueryHelper queryHelper=new QueryHelper(User.class,"u"); if(user != null){ if(StringUtils.isNotBlank(user.getName())){ user.setName(URLDecoder.decode(user.getName(),"utf-8")); queryHelper.addCondition("u.name like ?", "%"+user.getName()+"%"); } } pageResult=userService.getPageResult(queryHelper,getPageNo(),getPageSize()); } catch (Exception e) { throw new Exception(e.getMessage()); } return "listUI"; }还需要加一个strName来防止用户名查询条件被覆盖:
private String strName; public String getStrName() { return strName; } public void setStrName(String strName) { this.strName = strName; }
<result name="list" type="redirectAction"> <param name="actionName">user_listUI</param> <param name="user.name">${strName}</param> <param name="encode">true</param><!-- 需要编码 --> </result>
<s:iterator value="pageResult.items" status="st"> <!--中间代码不再赘述--> </s:iterator>
//列表页面 public String listUI() throws Exception{ try { //加载权限集合 ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP); QueryHelper queryHelper=new QueryHelper(Role.class,"r"); if(role != null){ if(StringUtils.isNotBlank(role.getName())){ role.setName(URLDecoder.decode(role.getName(),"utf-8")); queryHelper.addCondition("r.name like ?", "%"+role.getName()+"%"); } } pageResult=roleService.getPageResult(queryHelper,getPageNo(),getPageSize()); } catch (Exception e) { throw new Exception(e.getMessage()); } return "listUI"; }
<div class="c_pate" style="margin-top: 5px;"> <!-- 如果页数为空不显示分页选项 --> <s:if test="pageResult.totalCount > 0"> <table width="100%" class="pageDown" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="right"> 总共<s:property value="pageResult.totalCount"/>条记录,当前第 <s:property value="pageResult.pageNo"/> 页, 共 <s:property value="pageResult.totalPageCount"/> 页 <!-- 非第一页才有“上一页”选项 --> <s:if test="pageResult.pageNo>1"> <a href="javascript:doGoPage(<s:property value='pageResult.pageNo-1'/>)">上一页</a> </s:if> <!-- 非最后一页才有“下一页”选项 --> <s:if test="pageResult.pageNo < pageResult.totalPageCount"> <a href="javascript:doGoPage(<s:property value='pageResult.pageNo+1'/>)">下一页</a> </s:if> 到 <input id="pageNo" name="pageNo" type="text" style="width: 30px;" onkeypress="if(event.keyCode == 13){doGoPage(this.value);}" min="1" max="" value="<s:property value="pageResult.pageNo"/>" /> </td> </tr> </table> </s:if><s:else>暂无数据!</s:else> </div> <script type="text/javascript"> //翻页方法 function doGoPage(pageNo){ document.getElementById("pageNo").value = pageNo; document.forms[0].action="${basePath}tax/info_listUI.action"; document.forms[0].submit(); } </script> </div>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <div class="c_pate" style="margin-top: 5px;"> <!-- 如果页数为空不显示分页选项 --> <s:if test="pageResult.totalCount > 0"> <table width="100%" class="pageDown" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="right"> 总共<s:property value="pageResult.totalCount"/>条记录,当前第 <s:property value="pageResult.pageNo"/> 页, 共 <s:property value="pageResult.totalPageCount"/> 页 <!-- 非第一页才有“上一页”选项 --> <s:if test="pageResult.pageNo>1"> <a href="javascript:doGoPage(<s:property value='pageResult.pageNo-1'/>)">上一页</a> </s:if> <!-- 非最后一页才有“下一页”选项 --> <s:if test="pageResult.pageNo < pageResult.totalPageCount"> <a href="javascript:doGoPage(<s:property value='pageResult.pageNo+1'/>)">下一页</a> </s:if> 到 <input id="pageNo" name="pageNo" type="text" style="width: 30px;" onkeypress="if(event.keyCode == 13){doGoPage(this.value);}" min="1" max="" value="<s:property value="pageResult.pageNo"/>" /> </td> </tr> </table> </s:if><s:else>暂无数据!</s:else> </div> <script type="text/javascript"> //翻页方法 function doGoPage(pageNo){ document.getElementById("pageNo").value = pageNo; document.forms[0].action=list_url; document.forms[0].submit(); } </script>
function doSearch(){ //重置页号 $("#pageNo").val(1); document.forms[0].action = list_url; document.forms[0].submit(); }
<jsp:include page="/common/pageNavigator.jsp"></jsp:include>
工程代码:HpuTax2.0.zip
转载请注明出处:http://blog.csdn.net/acmman/article/details/49977021