Servlet+JSP+MyBatis 实现查询分页功能:首页、上一页、下一页、尾页
首先,我们需要创建一个 Servlet 来处理请求和响应。在这个 Servlet 中,我们可以使用 MyBatis 来执行查询操作,并将结果传递给 JSP 页面进行显示。\n\njava\n@WebServlet("/studentList")\npublic class StudentListServlet extends HttpServlet {\n protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n // 获取当前页数,默认为第一页\n int currentPage = 1;\n String pageStr = request.getParameter("currentPage");\n if (pageStr != null && !pageStr.isEmpty()) {\n currentPage = Integer.parseInt(pageStr);\n }\n\n // 设置每页显示的记录数\n int pageSize = 10;\n\n // 计算起始记录的索引\n int startIndex = (currentPage - 1) * pageSize;\n\n // 查询总记录数\n int totalCount = StudentDao.getTotalCount();\n\n // 查询当前页的数据\n List<Student> studentList = StudentDao.getStudentList(startIndex, pageSize);\n\n // 计算总页数\n int totalPages = (int) Math.ceil((double) totalCount / pageSize);\n\n // 将数据存储到 request 对象中\n request.setAttribute("studentList", studentList);\n request.setAttribute("totalCount", totalCount);\n request.setAttribute("totalPages", totalPages);\n request.setAttribute("currentPage", currentPage);\n\n // 转发到 JSP 页面进行显示\n request.getRequestDispatcher("studentList.jsp").forward(request, response);\n }\n}\n\n\n在这个 Servlet 中,我们首先获取当前页数和每页显示的记录数。然后,计算起始记录的索引,并调用 MyBatis 的方法来查询总记录数和当前页的数据。接下来,我们计算总页数,并将所有数据存储到 request 对象中。最后,我们将请求转发到 JSP 页面进行显示。\n\n接下来,我们需要创建一个 JSP 页面来显示查询结果和分页导航。\n\njsp\n<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>\n<!DOCTYPE html>\n<html>\n<head>\n <meta charset="UTF-8">\n <title>学生信息列表</title>\n</head>\n<body>\n <h1>学生信息列表</h1>\n <table>\n <tr>\n <th>学号</th>\n <th>姓名</th>\n <th>年龄</th>\n </tr>\n <%-- 遍历学生列表 --%>\n <c:forEach items="${studentList}" var="student">\n <tr>\n <td>${student.id}</td>\n <td>${student.name}</td>\n <td>${student.age}</td>\n </tr>\n </c:forEach>\n </table>\n\n <%-- 分页导航 --%>\n <%-- 首页 --%>\n <c:choose>\n <c:when test="${currentPage > 1}">\n <a href="studentList?currentPage=1">首页</a>\n </c:when>\n <c:otherwise>\n 首页\n </c:otherwise>\n </c:choose>\n\n <%-- 上一页 --%>\n <c:choose>\n <c:when test="${currentPage > 1}">\n <a href="studentList?currentPage=${currentPage - 1}">上一页</a>\n </c:when>\n <c:otherwise>\n 上一页\n </c:otherwise>\n </c:choose>\n\n <%-- 当前页 --%>\n <p>当前页:${currentPage}</p>\n\n <%-- 下一页 --%>\n <c:choose>\n <c:when test="${currentPage < totalPages}">\n <a href="studentList?currentPage=${currentPage + 1}">下一页</a>\n </c:when>\n <c:otherwise>\n 下一页\n </c:otherwise>\n </c:choose>\n\n <%-- 尾页 --%>\n <c:choose>\n <c:when test="${currentPage < totalPages}">\n <a href="studentList?currentPage=${totalPages}">尾页</a>\n </c:when>\n <c:otherwise>\n 尾页\n </c:otherwise>\n </c:choose>\n\n <%-- 总记录数和总页数 --%>\n <p>总记录数:${totalCount}</p>\n <p>总页数:${totalPages}</p>\n</body>\n</html>\n\n\n在这个 JSP 页面中,我们首先遍历学生列表,并显示每个学生的学号、姓名和年龄。然后,我们根据当前页和总页数来显示分页导航。点击导航链接会通过 GET 请求将当前页作为参数传递给 Servlet,从而实现翻页效果。最后,我们显示总记录数和总页数。\n\n以上就是使用 Servlet、JSP 和 MyBatis 实现查询加分页效果的示例代码。需要注意的是,示例中使用了 JSTL 标签库来简化 JSP 页面的编写,需要在项目中添加对应的 JAR 包。另外,需要根据实际情况修改 StudentDao 的方法来执行查询操作。
原文地址: https://www.cveoy.top/t/topic/p6B3 著作权归作者所有。请勿转载和采集!