package com.app.web.action; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.app.bean.Book; import com.app.pagination.Pagination; import com.app.service.BookService; import com.app.service.impl.BookServiceImpl; public class BookAction extends HttpServlet { private static final long serialVersionUID = 5275929408058702210L; private BookService bookService = new BookServiceImpl(); @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); int pageIndex = 1; int pageSize = 10; try { pageIndex = Integer.parseInt(request.getParameter("pageIndex")); pageSize = Integer.parseInt(request.getParameter("pageSize")); } catch (NumberFormatException e) { e.printStackTrace(); } //6: 显示的分页链接个数 Pagination<Book> bookPagination = bookService.getBookList(pageIndex, pageSize,6); request.setAttribute("bookPagination", bookPagination); request.getRequestDispatcher("index.jsp").forward(request, response); } }
Jsp
index.jsp
将Pagiation应用到bootstrap上的简单示例bootstrap版本: 3.3.5
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:if test="${requestScope.bookPagination == null }"> <c:redirect url="bookAction?pageIndex=1&pageSize=4"></c:redirect> </c:if> <!DOCTYPE html"> <html> <head> <title>图书信息列表</title> <!-- Bootstrap v3.3.5 --> <link href="https://www.jb51.net/${pageContext.request.contextPath}/bootstrap-3.3.5-dist/css/bootstrap.min.css" type="text/css" charset="utf-8" /> <link href="https://www.jb51.net/${pageContext.request.contextPath}/bootstrap-3.3.5-dist/css/bootstrap-theme.min.css" type="text/css" charset="utf-8" /> <script src="https://www.jb51.net/${pageContext.request.contextPath}/bootstrap-3.3.5-dist/js/jquery.min.js" type="text/javascript" charset="utf-8"></script> <script src="https://www.jb51.net/${pageContext.request.contextPath}/bootstrap-3.3.5-dist/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div> <h2>图书信息</h2> <table> <tr> <th>#</th> <th>图书名</th> <th>单价</th> </tr> <c:set var="bookPagination" value="${requestScope.bookPagination}"></c:set> <c:choose> <c:when test="${bookPagination.totalElements gt 0}"> <c:forEach var="book" items="${bookPagination.currData}"> <tr> <td>${book.id }</td> <td>${book.name }</td> <td>${book.price }</td> </tr> </c:forEach> <td colspan="3"> <div role="group"> <c:if test="${bookPagination.first}" var="isFirst"> <a disabled="disabled" href="#">首页</a> <a disabled="disabled" href="#">上一页</a> </c:if> <c:if test="${not isFirst}"> <a href="${pageContext.request.contextPath}/bookAction?pageIndex=1&pageSize=${bookPagination.pageSize}">首页</a> <a href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.previousIndex }&pageSize=${bookPagination.pageSize}">上一页</a> </c:if> <c:if test="${bookPagination.last }" var="isLast"> <a disabled="disabled" href="#">下一页</a> <a disabled="disabled" href="#">尾页</a> </c:if> <c:if test="${not isLast}"> <a href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.nextIndex }&pageSize=${bookPagination.pageSize}">下一页</a> <a href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.totalPages }&pageSize=${bookPagination.pageSize}">尾页</a> </c:if> </div> </td> </c:when> <c:otherwise> <tr><td colspan="3">没有更多数据!</td></tr> </c:otherwise> </c:choose> </table> <center> <nav> <ul> <c:if test="${isFirst }"> <li> <a href="#" aria-label="Previous"> <span aria-hidden="true">»上一页</span> </a> </li> </c:if> <c:if test="${not isFirst }"> <li> <a href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.previousIndex }&pageSize=${bookPagination.pageSize}" aria-label="Previous"> <span aria-hidden="true">»上一页</span> </a> </li> </c:if> <c:if test="${bookPagination.pageLinkNumber gt 0}"> <c:set var="betweenIndex" value="${bookPagination.betweenIndex}"></c:set> <c:forEach var="linkIndex" begin="${betweenIndex.beginIndex}" end="${betweenIndex.endIndex}"> <c:if test="${linkIndex eq bookPagination.pageIndex}" var="isCurr"> <li><a href="#">${linkIndex}</a></li> </c:if> <c:if test="${not isCurr}"> <li><a href="${pageContext.request.contextPath}/bookAction?pageIndex=${linkIndex}&pageSize=${bookPagination.pageSize}" >${linkIndex}</a></li> </c:if> </c:forEach> </c:if> <c:if test="${isLast }"> <li> <a href="#" aria-label="Next"> <span aria-hidden="true">下一页 »</span> </a> </li> </c:if> <c:if test="${not isLast }"> <li> <a href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.nextIndex }&pageSize=${bookPagination.pageSize}" aria-label="Next"> <span aria-hidden="true">下一页 »</span> </a> </li> </c:if> </ul> </nav> </center> </div> </body> </html>
实例数据
说明:
如果需要扩展分页功能, 请扩展Pagiation接口以及其余实现类;
此外, 此分页不依赖任何数据库(重新实现QueryHandler查询回调接口即可), 可适用于任何web项目中;
使用List 集合模拟数据库的使用示例: