前五后四分页条的实现_jsp页面 程序员文章站 2022-03-12 19:44:47 jsp页面代码 jsp页面代码 <div> <nav aria-label="Page navigation"> <ul class="pagination"> <c:if test="${pb.currentPage == 1}"> <li class="disabled"> <a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${pb.currentPage - 1}&rows=5" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> </c:if> <c:if test="${pb.currentPage != 1}"> <li class="active"> <a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${pb.currentPage - 1}&rows=5" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> </c:if> <%--将域中的begin与end传入即可--%> <c:forEach begin="${pb.begin}" end="${pb.end}" var="i"> <c:if test="${i == pb.currentPage}"> <li class="active"> <a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${i}&rows=5">${i}</a> </li> </c:if> <c:if test="${i != pb.currentPage}"> <li> <a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${i}&rows=5">${i}</a> </li> </c:if> </c:forEach> <c:if test="${pb.currentPage == pb.totalPage}"> <li class="disabled"> <a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${pb.currentPage + 1}&rows=5" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </c:if> <c:if test="${pb.currentPage != pb.totalPage}"> <li class="active"> <a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${pb.currentPage + 1}&rows=5" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </c:if> <span style="font-size: 25px;margin-left: 5px;"> 共${pb.totalCount}条记录,共${pb.totalPage}页 </span> </ul> </nav> </div> 服务器代码实现 web层 @WebServlet("/findPageUserServlet") public class FindPageUserServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String currentPage = request.getParameter("currentPage"); String rows = request.getParameter("rows"); //设置第一次页码为1. if (currentPage == null || currentPage.trim().length() == 0){ currentPage = "1"; } if (rows == null || rows.trim().length() == 0){ rows = "5"; } Map<String, String[]> map = request.getParameterMap(); UserService service = new UserServiceImpl(); PageBean<User> pb = service.findPageBean(currentPage,rows,map); //实现前五后四 request.setAttribute("pb",pb); request.getRequestDispatcher("/list.jsp").forward(request,response); } service层 public class UserServiceImpl implements UserService { private UserDao dao = new UserDaoImpl(); @Override public PageBean<User> findPageBean(String _currentPage, String _rows, Map<String, String[]> map) { //设置参数 int currentPage = Integer.parseInt(_currentPage); int rows = Integer.parseInt(_rows); if (currentPage <= 0 ){ currentPage = 1; } pb.setCurrentPage(currentPage); pb.setRows(rows); //总页数 int totalCount = dao.totalCount(map); pb.setTotalCount(totalCount); //总页码 int totalPage = totalCount % rows == 0 ? totalCount/rows : totalCount/rows+1; pb.setTotalPage(totalPage); //前五后四效果 int begin; int end; if (totalPage < 10){ begin = 1; end = totalPage; } else { begin = currentPage - 5; end = currentPage + 4; if ( begin < 1 ){ begin = 1; end = begin + 9; } if (end > totalPage){ end = totalPage; begin = end - 9 ; } } pb.setBegin(begin); pb.setEnd(end); return pb; } 本文地址:https://blog.csdn.net/weixin_45958862/article/details/107280788 上一篇: 基于SpringMVC入门案例及讲解 下一篇: CSS精华总结之css中的可继承属性! 推荐阅读 前五后四分页条的实现_jsp页面 【JSP篇】——cookie之商品浏览记录的实现:5.cookie实现前五条浏览记录 前五后四分页条的实现_jsp页面