分页的一小段代码,比较健忘,存着自己用

本文介绍了一种分页逻辑的实现方法,并通过Java和JSP代码展示了如何根据当前页数动态调整显示的页码范围。该方法考虑了不同情况下的页面布局,如总页数较少或当前页接近总页数时的特殊处理。
				int total = Integer.parseInt(totalNum);
				int allPages = total % 10 == 0 ? total / 10 : total / 10 + 1;

				int pageLength = allPages > 9 ? 9 : allPages;

				int pagearr[] = new int[pageLength];

				if (page > 4) {
					if (allPages - page > 4) {
						for (int i = 0; i < 4; i++) {
							pagearr[i] = page + i - 4;
							pagearr[5 + i] = page + i + 1;
						}
						pagearr[4] = page;
					} else if (allPages > 9) {
						for (int i = 0; i < pageLength; i++) {
							pagearr[i] = allPages + i - 8;
						}
					} else {
						for (int i = 0; i < pageLength; i++) {
							pagearr[i] = i + 1;
						}
					}
				} else {
					for (int i = 0; i < pageLength; i++) {
						pagearr[i] = i + 1;
					}
				}

 

jsp片断

 

<c:if test="${page>1}">
										<a
											href="<%=path%>/com/fullSeach.do?page=${page-1 }&q=${q }&type=${type }" class="prev-page">上一页
										</a>
									</c:if>
									<c:forEach items="${pages}" var="p">

										<c:choose>
											<c:when test="${p==page}">
												<span class="current-page">${p }</span>

											</c:when>
											<c:otherwise>
												<a
													href="<%=path%>/com/fullSeach.do?page=${p }&q=${q }&type=${type }">${p
													}</a>
											</c:otherwise>
										</c:choose>
									</c:forEach>

									<c:if test="${page<maxPage}">
										<a
											href="<%=path%>/com/fullSeach.do?page=${page+1 }&q=${q }&type=${type }" class="next-page">下一页
										</a>
									</c:if>
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值