web开发中经常需要用到分页,我然来的做法是每次需要分页时都把代码copy一份,比如有10个页面有分页,那么我分页的代码就有10个版本.这样导致代码的重用性太低了.
那么如何解决呢?
把分页的页面和逻辑抽取出来,提高代码质量和重用性.
(1)分页的页面抽取出来
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<table style="height: 35px">
<tr>
<td nowrap="nowrap" style="width: 6%">
<td nowrap="nowrap" style="width: 63%"><span>共<s:property
value="view.totalRecords" />条记录, 当前第 <s:if
test="view.totalPages==0">0</s:if> <s:else>
<s:property value="view.currentPage" />
</s:else>/ <s:property value="view.totalPages" />页, 每页<s:property
value="view.recordsPerPage" />条记录
</span></td>
<td nowrap="nowrap">
<button type="button" class="btn btn-info" style="padding: 1px 10px"
οnclick="toPageFirst(${param.action})">首页</button>
<button type="button" class="btn btn-info" style="padding: 1px 10px"
οnclick="toPagePre(${param.action})">上一页</button>
<button type="button" class="btn btn-info" style="padding: 1px 10px"
οnclick="toPageNext(${param.action})">下一页</button>
<button type="button" class="btn btn-info" style="padding: 1px 10px"
οnclick="toPageLast(${param.action})">尾页</button>
<button type="button" class="btn btn-info" style="padding: 1px 10px"
οnclick="toPageGo(${param.action})">转</button>
<s:if test="view.totalPages==0">
<input id="view.currentPage"
name="view.currentPage" size="5"
style="margin-bottom: 0px; width: 50px; ime-mode: disabled; text-align: right; padding: 0px 1px 0px 0px; height: 20px; display: inline-block"
value="0" />
</s:if> <s:else>
<s:textfield cssClass="form-control" id="view.currentPage"
name="view.currentPage" size="5"
οnkeypress="return onlyNumber(event);" οnpaste="return false;"
style="margin-bottom: 0px; width: 50px; ime-mode: disabled; text-align: right; padding: 0px 1px 0px 0px; height: 20px; display: inline-block"
/>
</s:else>
<input type="hidden" id="view.thisPage" value="<s:property value='view.currentPage' />" />
<s:hidden id="view.totalPages" name="view.totalPages"></s:hidden>
<s:hidden id="view.ascDesc" name="view.ascDesc"></s:hidden>
<s:hidden id="view.sortKey" name="view.sortKey"></s:hidden>
</td>
<td nowrap="nowrap"><span>页</span></td>
</tr>
</table>
(2)在需要分页的页面中引入上述jsp文件
<s:include value="/WEB-INF/jsp/pageBottom.jsp">
<s:param name="action">usercoupon.query</s:param>
</s:include>
注意:必须包含在form表单中
(3)在后台action中
public String execute() throws Exception {
reserveCondition();
int start = (Integer.parseInt(view.getCurrentPage()) - 1)
* view.getRecordsPerPage();
int count = 0;
DetachedCriteria clientVersionDC = DetachedCriteria
.forClass(User.class);
condition(clientVersionDC);
List list = new ArrayList();
count = this.userService.listByDetachedCriteria(list,
clientVersionDC, start, view.getRecordsPerPage());
view.setRecordList(list);
queryResultList = new ArrayList();
queryResultList.addAll(view.getRecordList());
view.setTotalRecords(count);
int totalPages = PageUtil.getTotalPages(view.getTotalRecords(),
view.getRecordsPerPage());
view.setTotalPages(String.valueOf(totalPages));
return "list";
}
在js中声明方法时,要注意,左边是错误的