java web 分页解决方案

236 篇文章 0 订阅

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中声明方法时,要注意,左边是错误的

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值