分页查询

本文介绍了两种分页技术:真分页和假分页,并详细解释了它们的工作原理及实现方式。通过实例展示了如何在Java中使用这两种分页方法来处理大量数据的展示问题。

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

/**
* 真分页
* @param
* @param req
* @return
* @return 对象page
*/
private static Page truePaging (int allRecord,HttpServletRequest req){
//设置每页显示数
int pageSizeStr = 5;
// 得到当前页数
String pageIndexStr = req.getParameter("pageIndex");
if (pageIndexStr == null) {
pageIndexStr = "1";
}
int pageIndex = Integer.parseInt(pageIndexStr);
req.setAttribute("pageIndex", pageIndex);

//得到总页数
int allPage = (allRecord % pageSizeStr == 0) ? (allRecord / pageSizeStr)
: (allRecord / pageSizeStr + 1);
req.setAttribute("allPage", allPage);

//返回总共记录数
req.setAttribute("allRecord", allRecord) ;
int toIndex = pageSizeStr * (pageIndex);
// 判断是不是最9后一页
if (pageIndex == allPage) {
toIndex = allRecord;
}
Page page = new Page();
page.setFirstline(pageSizeStr * (pageIndex - 1));
page.setLastline(toIndex);
return page;
}

/**
* 假分页
* @param list
* @param req
* @return
*/
private static List paging(List list,HttpServletRequest req){
//设置每页显示数
int pageSizeStr = 5;
// 得到当前页数
String pageIndexStr = req.getParameter("pageIndex");
if (pageIndexStr == null) {
pageIndexStr = "1";
}
int pageIndex = Integer.parseInt(pageIndexStr);
req.setAttribute("pageIndex", pageIndex);

//得到总页数
int allPage = (list.size() % pageSizeStr == 0) ? (list.size() / pageSizeStr)
: (list.size() / pageSizeStr + 1);
req.setAttribute("allPage", allPage);
//返回总共记录数
req.setAttribute("allRecord", list.size()) ;
int toIndex = pageSizeStr * (pageIndex);
// 判断是不是最后一页
if (pageIndex == allPage) {
toIndex = list.size();
}
List list2 = list.subList(pageSizeStr * (pageIndex - 1),toIndex);
return list2;
}

真分页的调用

Page page =truePaging(allRecord,req);
book.setPage(page);

list = adminBiz.findBookAll(book);
if(list.size() == 0){
req.setAttribute("allRecord", 0);
return "/All_book.jsp";
}
req.setAttribute("bookInfo", book);
req.setAttribute("bookList",list);

return "/All_book.jsp";

假分页

List<Inform> list2 = paging(list, req);
req.setAttribute("informAll", list2);
return "/pbshow_list.jsp";

页面样式

<!-- 分页样式 -->
<div class="pagin">
<div class="message">
共<i class="blue">${allRecord}</i>条记录,当前显示第&nbsp;<i class="blue">${pageIndex}&nbsp;</i>页
</div>
<ul class="paginList">
<c:if test="${pageIndex>1}">
<li class="paginItem"><a
href="adminAction/selBook.action?pageIndex=1&booktypeid=${param.booktypeid}&bookname=${param.bookname}&author=${param.author}&code=${param.code}&intime=${param.intime}&pubhou=${param.pubhou}&bshelfid=${param.bshelfid}" style="width: 50px">首页</a>
</li>
<li class="paginItem"><a
href="adminAction/selBook.action?pageIndex=${pageIndex-1 }&booktypeid=${param.booktypeid}&bookname=${param.bookname}&author=${param.author}&code=${param.code}&intime=${param.intime}&pubhou=${param.pubhou}&bshelfid=${param.bshelfid}"
style="width: 50px">上一页</a>
</li>

</c:if>
<c:if test="${pageIndex<10}">
<c:set var="startPage" value="1" />
<c:if test="${allPage<=10}">
<c:set var="endPage" value="${allPage }" />
</c:if>
<c:if test="${allPage>10}">
<c:set var="endPage" value="10" />
</c:if>
</c:if>
<c:if test="${pageIndex>=10}">
<c:if test="${allPage>(pageIndex+5)}">
<c:set var="startPage" value="${pageIndex-5}" />
<c:set var="endPage" value="${pageIndex+5}" />
</c:if>
<c:if test="${allPage<=(pageIndex+5)}">
<c:set var="startPage" value="${allPage-10}" />
<c:set var="endPage" value="${allPage }" />
</c:if>
</c:if>
<c:forEach var="num" begin="${startPage}" end="${endPage }" step="1">
<c:if test="${pageIndex==num}">
<li class="paginItem current"><a>${num}</a>
</li>
</c:if>
<c:if test="${pageIndex!=num}">
<li class="paginItem"><a
href="adminAction/selBook.action?pageIndex=${num}&booktypeid=${param.booktypeid}&bookname=${param.bookname}&author=${param.author}&code=${param.code}&intime=${param.intime}&pubhou=${param.pubhou}&bshelfid=${param.bshelfid}">${num}</a>
</li>
</c:if>
</c:forEach>
<c:if test="${pageIndex<allPage}">
<li class="paginItem"><a
href="adminAction/selBook.action?pageIndex=${pageIndex+1 }&booktypeid=${param.booktypeid}&bookname=${param.bookname}&author=${param.author}&code=${param.code}&intime=${param.intime}&pubhou=${param.pubhou}&bshelfid=${param.bshelfid}"
style="width: 50px">下一页</a>
</li>
<li class="paginItem"><a
href="adminAction/selBook.action?pageIndex=${allPage}&booktypeid=${param.booktypeid}&bookname=${param.bookname}&author=${param.author}&code=${param.code}&intime=${param.intime}&pubhou=${param.pubhou}&bshelfid=${param.bshelfid}" style="width: 50px">末页</a>
</li>
</c:if>

</ul>
</div>
</div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值