JSTL实现分页

本文详细介绍了在网站中如何设计并实现优雅的分页导航功能。通过具体的例子展示了不同页面下分页导航的显示逻辑,并提供了实现这些功能的具体代码片段。文章重点在于确保用户体验的同时,兼顾了开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最多显示左右三页间隔,首页和末页必显示
1页 
当前任意页  1
2页 
当前任意页  1 2
3页 
当前任意页  1 2 3
4页 
当前任意页  1 2 3 4
5页 
当前任意页  1 2 3 4 5
6页 
当前第1页  1 2 3 4 ... 6
当前第2-5页 1 2 3 4 5 6
当前第6页  1 ... 3 4 5 6
7页 
当前第1页  1 2 3 4 ... 7
当前第2页  1 2 3 4 5 ... 7
当前第3-5页 1 2 3 4 5 6 7
当前第6页  1 ...  3 4 5 6 7
当前第7页  1 ... 4 5 6 7
8页
当前第1页  1 2 3 4 ... 8
当前第2页  1 2 3 4 5 ... 8
当前第3页  1 2 3 4 5 6 ... 8
当前第4-5页 1 2 3 4 5 6 7 8
当前第6页  1 ... 3 4 5 6 7 8
当前第7页  1 ... 4 5 6 7 8
当前第8页  1 ... 5 6 7 8
9页
当前第1页  1 2 3 4 ... 9
当前第2页  1 2 3 4 5 ... 9
当前第3页  1 2 3 4 5 6 ... 9
当前第4页  1 2 3 4 5 6 7 ... 9
当前第5页  1 2 3 4 5 6 7 8 9
当前第6页  1 ... 3 4 5 6 7 8 9
当前第7页  1 ... 4 5 6 7 8 9
当前第8页  1 ... 5 6 7 8 9
当前第9页  1 ... 6 7 8 9
10页

当前第1页  1 2 3 4 ... 10
当前第2页  1 2 3 4 5 ... 10
当前第3页  1 2 3 4 5 6 ... 10
当前第4页  1 2 3 4 5 6 7 ... 10
当前第5页  1 2 3 4 5 6 7 8 ... 10
当前第6页  1 ... 3 4 5 6 7 8 9 10
当前第7页  1 ... 4 5 6 7 8 9 10
当前第8页  1 ... 5 6 7 8 9 10
当前第9页  1 ... 6 7 8 9 10
当前第10页 1 ... 7 8 9 10
11页
当前第1页  1 2 3 4 ... 11
当前第2页  1 2 3 4 5 ... 11
当前第3页  1 2 3 4 5 6 ... 11
当前第4页  1 2 3 4 5 6 7 ... 11
当前第5页  1 2 3 4 5 6 7 8 ... 11
当前第6页  1 ... 3 4 5 6 7 8 9 ... 11
当前第7页  1 ... 4 5 6 7 8 9 10 11
当前第8页  1 ... 5 6 7 8 9 10 11
当前第9页  1 ... 6 7 8 9 10 11
当前第10页 1 ... 7 8 9 10 11
当前第11页 1 ... 8 9 10 11
12页
当前第1页  1 2 3 4 ... 12
当前第2页  1 2 3 4 5 ... 12
当前第3页  1 2 3 4 5 6 ... 12
当前第4页  1 2 3 4 5 6 7 ... 12
当前第5页  1 2 3 4 5 6 7 8 ... 12
当前第6页  1 ... 3 4 5 6 7 8 9 ... 12
当前第7页  1 ... 4 5 6 7 8 9 10 ... 12
当前第8页  1 ... 5 6 7 8 9 10 11 12
当前第9页  1 ... 6 7 8 9 10 11 12
当前第10页 1 ... 7 8 9 10 11 12
当前第11页 1 ... 8 9 10 11 12
当前第12页 1 ... 9 10 11 12

 

实现方案:
总页数pageAmount
当前页码currentPage
页数左间隔/右间隔interval
(1)第一页和最后一页肯定显示n=pageAmount,当n=1时只显示1页
(2)中间页码显示
如果currentPage<=5,从2显示至currentPage
如果currentPage>5,显示省略号,然后从currentPage-3显示至currentPage
如果currentPage<=pageAmount-4或者pageAmount-4<=0,从currentPage+1显示至pageAmount
如果currentPage>pageAmount-4,从currentPage+1显示至currentPage+3,然后显示省略号和pageAmount

 

每页显示&nbsp;${sessionScope.count}&nbsp;条记录&nbsp;
总记录数/总页数&nbsp;${sessionScope.recordAmount}/${sessionScope.pageAmount}
&nbsp;当前第&nbsp;
<a href="ViewServlet?currentPage=1">[1]</a>&nbsp;
<c:if test="${sessionScope.pageAmount!=1}">
	<c:choose>
		<c:when test="${requestScope.currentPage<=5}">
			<c:forEach var="i" begin="2" end="${requestScope.currentPage}">
				<a href="ViewServlet?currentPage=${i}">[${i }]</a>&nbsp;
			</c:forEach>
		</c:when>
		<c:otherwise>
			...&nbsp;
			<c:forEach var="i" begin="${requestScope.currentPage-3}"
				end="${requestScope.currentPage}">
				<a href="ViewServlet?currentPage=${i}">[${i }]</a>&nbsp;
			</c:forEach>
		</c:otherwise>
	</c:choose>
	<c:choose>
		<c:when test="${requestScope.currentPage>=sessionScope.pageAmount-4 
			|| sessionScope.pageAmount-4<=0}">
			<c:forEach var="i" begin="${requestScope.currentPage+1}"
				end="${sessionScope.pageAmount}">
				<a href="ViewServlet?currentPage=${i}">[${i }]</a>&nbsp;
			</c:forEach>
		</c:when>
		<c:otherwise>
			<c:forEach var="i" begin="${requestScope.currentPage+1}"
				end="${requestScope.currentPage+3}">
				<a href="ViewServlet?currentPage=${i}">[${i }]</a>&nbsp;
			</c:forEach>
			...&nbsp;
			<a href="ViewServlet?currentPage=${sessionScope.pageAmount}">
				[${sessionScope.pageAmount}]</a>&nbsp;
		</c:otherwise>
	</c:choose>
</c:if>
页

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值