首先,我们需要创建一个Servlet来处理请求和响应。在这个Servlet中,我们可以使用MyBatis来执行查询操作,并将结果传递给JSP页面进行显示。

@WebServlet("/studentList")
public class StudentListServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取当前页数,默认为第一页
        int currentPage = 1;
        String pageStr = request.getParameter("currentPage");
        if (pageStr != null && !pageStr.isEmpty()) {
            currentPage = Integer.parseInt(pageStr);
        }

        // 设置每页显示的记录数
        int pageSize = 10;

        // 计算起始记录的索引
        int startIndex = (currentPage - 1) * pageSize;

        // 查询总记录数
        int totalCount = StudentDao.getTotalCount();

        // 查询当前页的数据
        List<Student> studentList = StudentDao.getStudentList(startIndex, pageSize);

        // 计算总页数
        int totalPages = (int) Math.ceil((double) totalCount / pageSize);

        // 将数据存储到request对象中
        request.setAttribute("studentList", studentList);
        request.setAttribute("totalCount", totalCount);
        request.setAttribute("totalPages", totalPages);
        request.setAttribute("currentPage", currentPage);

        // 转发到JSP页面进行显示
        request.getRequestDispatcher("studentList.jsp").forward(request, response);
    }
}

在这个Servlet中,我们首先获取当前页数和每页显示的记录数。然后,计算起始记录的索引,并调用MyBatis的方法来查询总记录数和当前页的数据。接下来,我们计算总页数,并将所有数据存储到request对象中。最后,我们将请求转发到JSP页面进行显示。

接下来,我们需要创建一个JSP页面来显示查询结果和分页导航。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>学生信息列表</title>
</head>
<body>
    <h1>学生信息列表</h1>
    <table>
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
        <%-- 遍历学生列表 --%>
        <c:forEach items="${studentList}" var="student">
            <tr>
                <td>${student.id}</td>
                <td>${student.name}</td>
                <td>${student.age}</td>
            </tr>
        </c:forEach>
    </table>

    <%-- 分页导航 --%>
    <%-- 首页 --%>
    <c:choose>
        <c:when test="${currentPage > 1}">
            <a href="studentList?currentPage=1">首页</a>
        </c:when>
        <c:otherwise>
            首页
        </c:otherwise>
    </c:choose>

    <%-- 上一页 --%>
    <c:choose>
        <c:when test="${currentPage > 1}">
            <a href="studentList?currentPage=${currentPage - 1}">上一页</a>
        </c:when>
        <c:otherwise>
            上一页
        </c:otherwise>
    </c:choose>

    <%-- 当前页 --%>
    <p>当前页:${currentPage}</p>

    <%-- 下一页 --%>
    <c:choose>
        <c:when test="${currentPage < totalPages}">
            <a href="studentList?currentPage=${currentPage + 1}">下一页</a>
        </c:when>
        <c:otherwise>
            下一页
        </c:otherwise>
    </c:choose>

    <%-- 尾页 --%>
    <c:choose>
        <c:when test="${currentPage < totalPages}">
            <a href="studentList?currentPage=${totalPages}">尾页</a>
        </c:when>
        <c:otherwise>
            尾页
        </c:otherwise>
    </c:choose>

    <%-- 总记录数和总页数 --%>
    <p>总记录数:${totalCount}</p>
    <p>总页数:${totalPages}</p>
</body>
</html>

在这个JSP页面中,我们首先遍历学生列表,并显示每个学生的学号、姓名和年龄。然后,我们根据当前页和总页数来显示分页导航。点击导航链接会通过GET请求将当前页作为参数传递给Servlet,从而实现翻页效果。最后,我们显示总记录数和总页数。

以上就是使用Servlet、JSP和MyBatis实现查询加分页效果的示例代码。需要注意的是,示例中使用了JSTL标签库来简化JSP页面的编写,需要在项目中添加对应的JAR包。另外,需要根据实际情况修改StudentDao的方法来执行查询操作

使用servlet+jsp+mybatis1实现查询加分页效果首页上一页下一页尾页

原文地址: https://www.cveoy.top/t/topic/ioKI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录