1.创建分页实体类
public class PageBean<T> {
private List<T> list;// 当前页内容 查询
private int currPage;// 当前页码 传递
private int pageSize;// 每页显示的条数 固定
private int totalCount;// 总条数 查询
private int totalPage;// 总页数 计算
。。。。。
/**
* 获取总页数
*
* @return
*/
public int getTotalPage() {
return (int) Math.ceil(totalCount * 1.0 / pageSize);
}
public PageBean() {
}
public PageBean(List<T> list, int currPage, int pageSize, int totalCount) {
super();
this.list = list;
this.currPage = currPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
}
}
2.service调Dao分别获取查询当页的数据list和总数count
将list和count等数据封装到PageBean中
3.servlet通过service获取数据返回到前台展示
分页部分:
<center>
<!-- 若是第一页,首页和上一页不显示 -->
<c:if test="${pb.currPage!=1 }">
<a
href="${pageContext.request.contextPath }/showProductsByPage?currPage=1">[首页]</a>
<a
href="${pageContext.request.contextPath }/showProductsByPage?currPage=${pb.currPage-1}">[上一页]</a>
</c:if>
<!-- 将所有的页码显示出来 -->
<c:forEach begin="1" end="${pb.totalPage }" var="n">
<c:if test="${pb.currPage==n }">
${n }
</c:if>
<c:if test="${pb.currPage!=n }">
<a
href="${pageContext.request.contextPath }/showProductsByPage?currPage=${pb.currPage}">${n }</a>
</c:if>
</c:forEach>
<!-- 若是最后一页,末页和下一页不显示 -->
<c:if test="${pb.currPage!=pb.totalPage }">
<a
href="${pageContext.request.contextPath }/showProductsByPage?currPage=${pb.currPage+1}">[下一页]</a>
<a
href="${pageContext.request.contextPath }/showProductsByPage?currPage=${pb.totalPage}">[末页]</a>
</c:if>
</center>