jsp页面分页功能(首页、下一页、上一页、末页)
时间: 2023-05-30 10:07:10 浏览: 363
1. 首先,需要在后台代码中获取数据总量,并计算出总页数。
2. 在jsp页面中,引入Bootstrap框架的分页组件,如下所示:
```html
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
```
3. 在jsp页面中,根据当前页数和总页数,生成分页链接。同时,在链接中传递当前页数参数,在后台代码中根据参数获取对应页的数据。
```html
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="?pageNum=1" aria-label="First">
<span aria-hidden="true"><<</span>
</a>
</li>
<li>
<a href="?pageNum=${page.currentPage-1}" aria-label="Previous">
<span aria-hidden="true"><</span>
</a>
</li>
<c:forEach var="i" begin="1" end="${page.totalPage}">
<c:choose>
<c:when test="${i == page.currentPage}">
<li class="active"><a href="#">${i}</a></li>
</c:when>
<c:otherwise>
<li><a href="?pageNum=${i}">${i}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
<li>
<a href="?pageNum=${page.currentPage+1}" aria-label="Next">
<span aria-hidden="true">></span>
</a>
</li>
<li>
<a href="?pageNum=${page.totalPage}" aria-label="Last">
<span aria-hidden="true">>></span>
</a>
</li>
</ul>
</nav>
```
4. 在后台代码中,根据当前页数和每页数据量,计算出需要查询的数据范围,并查询出数据列表。同时,将分页信息封装成Page对象,传递给jsp页面。
```java
int pageNum = Integer.parseInt(request.getParameter("pageNum"));
int pageSize = 10;
int start = (pageNum - 1) * pageSize;
int end = start + pageSize;
List<Data> dataList = getDataList(start, end);
int total = getTotalCount();
int totalPage = (int) Math.ceil((double) total / pageSize);
Page page = new Page();
page.setCurrentPage(pageNum);
page.setPageSize(pageSize);
page.setDataList(dataList);
page.setTotalCount(total);
page.setTotalPage(totalPage);
request.setAttribute("page", page);
request.getRequestDispatcher("data-list.jsp").forward(request, response);
```
阅读全文