思路:建一个分页的实体类,包含属性:总数据数,总页数,满足条件的List集合,当前页,每页显示的条数。封装对象,通过servlet转发到前端jsp界面,使用JSTL渲染界面数据。
效果
前端
<nav aria-label="Page navigation">
<ul class="pagination">
<c:if test="${pageBean.currentPage==1}">
<li class="disabled">
</c:if>
<c:if test="${pageBean.currentPage!=1}">
<li>
</c:if>
<a href="${pageContext.request.contextPath}/bookInfoList?currentPage=${pageBean.currentPage-1}"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<c:if test="${pageBean.currentPage==1}">
<li><a style="background: #2aabd2" href="${pageContext.request.contextPath}/bookInfoList?currentPage=1">1</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage!=1}">
<li><a href="${pageContext.request.contextPath}/bookInfoList?currentPage=1">1</a></li>
</c:if>
<c:forEach begin="2" end="${pageBean.totalPage}" var="i">
<c:if test="${pageBean.currentPage==i}">
<li><a style="background: #2aabd2"
href="${pageContext.request.contextPath}/bookInfoList?currentPage=${i}">${i}</a></li>
</c:if>
<c:if test="${pageBean.currentPage!=i}">
<li><a href="${pageContext.request.contextPath}/bookInfoList?currentPage=${i}">${i}</a></li>
</c:if>
</c:forEach>
<c:if test="${pageBean.currentPage!=pageBean.totalPage}">
<li>
</c:if>
<c:if test="${pageBean.currentPage==pageBean.totalPage}">
<li class="disabled">
</c:if>
<a href="${pageContext.request.contextPath}/bookInfoList?currentPage=${pageBean.currentPage+1}"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
<strong>当前一共有${pageBean.totalCount}条数据,共${pageBean.totalPage}页</strong>
</ul>
</nav>
servlet
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
String currentPage = req.getParameter("currentPage");
String rows = req.getParameter("rows");
if (currentPage == null || "".equals(currentPage)) {
currentPage = "1";
}
if (Integer.parseInt(currentPage) <= 0) {
currentPage = "1";
}
if (rows == null || "".equals(rows)) {
rows = "4";
}
PageBean<BookInfo> PageBean = bookInfoService.BookListByPage(currentPage, rows);
req.setAttribute("pageBean", PageBean);
req.getRequestDispatcher("bookinfolist.jsp").forward(req, resp);
}
PageBean实体类
package com.library.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageBean<T> {
private int totalCount;//总共数据的条数
private int totalPage;//总共数据的页数
private List<T> list;//每页数据的list集合
private int currentPage;//当前页码
private int rows;//每页显示的条数
}
service层
public PageBean<BookInfo> BookListByPage(String _currentPage, String _rows) {
int currentPage = Integer.parseInt(_currentPage);
int rows = Integer.parseInt(_rows);
PageBean<BookInfo> pageBean = new PageBean<BookInfo>();
pageBean.setRows(rows);
pageBean.setCurrentPage(currentPage);
int totalCount = bookInfoDao.findTotalCount();
pageBean.setTotalCount(totalCount);
int totalPage = (totalCount % rows) == 0 ? totalCount / rows : (totalCount / rows) + 1;
pageBean.setTotalPage(totalPage);
if (currentPage >= pageBean.getTotalPage()) {
currentPage = pageBean.getTotalPage();
}
pageBean.setCurrentPage(currentPage);
List<BookInfo> list = bookInfoDao.findList((currentPage - 1) * rows, rows);
pageBean.setList(list);
return pageBean;
}
dao层
@Override
public int findTotalCount() {
String sql = "select * from book_info";
return Utils.executeQuery(sql, null, BookInfo.class).size();
}
@Override
public List<BookInfo> findList(int i, int rows) {
String sql = "select * from book_info limit ?,?";
Object[] params = {i, rows};
return Utils.executeQuery(sql, params, BookInfo.class);
}