gpt4 book ai didi

java - 如何在JSP..中进行分页?

转载 作者:行者123 更新时间:2023-12-04 11:17:20 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





ResultSet to Pagination

(5 个回答)


6年前关闭。




请不要低估我的问题,因为我是 JSP/JavaEE 的新手。

我看了很多关于 JSTL、Pagination 的教程,但我无法正确地做 Pagination .. 有没有一种 Esay 方法来执行 Pagination ..?

在我的程序中,我使用 java 类从数据库中检索搜索结果的记录并将它们放入一个 ArrayList 并将 ArrayList 返回给 Servlet。然后将接收到的 ArrayList 对象分配给这样的应用程序属性

request.setAttribute("Results", results_List);

然后使用请求调度程序,我正在加载我的结果,显示像这样的 jsp 页面。
RequestDispatcher rd = request.getRequestDispatcher("searchresults.jsp");
rd.forward(request, response);

所以告诉我下一步做分页我的搜索结果..

最佳答案

在 Servlet 中,我们首先获取“page”参数的值并将其存储在“page”变量中。我们希望每页显示五 (5) 条记录,我们将这些记录作为参数传递给 viewAllEmployees(offset, 5)。我们在请求范围内存储三个属性并将请求转发到一个 JSP 页面;

服务类:

public void doGet(HttpServletRequest request,  HttpServletResponse response)  throws ServletException, IOException {
int page = 1;
int recordsPerPage = 5;
if(request.getParameter("page") != null)
page = Integer.parseInt(request.getParameter("page"));
EmployeeDAO dao = new EmployeeDAO();
List<Employee> list = dao.viewAllEmployees((page-1)*recordsPerPage,
recordsPerPage);
int noOfRecords = dao.getNoOfRecords();
int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
request.setAttribute("employeeList", list);
request.setAttribute("noOfPages", noOfPages);
request.setAttribute("currentPage", page);
RequestDispatcher view = request.getRequestDispatcher("employee.jsp");
view.forward(request, response);
}

Jsp文件:
<table border="1" cellpadding="5" cellspacing="5">
        <tr>
            <th>Emp ID</th>
            <th>Emp Name</th>
            <th>Salary</th>
            <th>Dept Name</th>
        </tr>
 
        <c:forEach var="employee" items="${employeeList}">
            <tr>
                <td>${employee.employeeId}</td>
                <td>${employee.employeeName}</td>
                <td>${employee.salary}</td>
                <td>${employee.deptName}</td>
            </tr>
        </c:forEach>
    </table>
 
    <%--For displaying Previous link except for the 1st page --%>
    <c:if test="${currentPage != 1}">
        <td><a href="employee.do?page=${currentPage - 1}">Previous</a></td>
    </c:if>
 
    <%--For displaying Page numbers.
    The when condition does not display a link for the current page--%>
    <table border="1" cellpadding="5" cellspacing="5">
        <tr>
            <c:forEach begin="1" end="${noOfPages}" var="i">
                <c:choose>
                    <c:when test="${currentPage eq i}">
                        <td>${i}</td>
                    </c:when>
                    <c:otherwise>
                        <td><a href="employee.do?page=${i}">${i}</a></td>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
        </tr>
    </table>
     
    <%--For displaying Next link --%>
    <c:if test="${currentPage lt noOfPages}">
        <td><a href="employee.do?page=${currentPage + 1}">Next</a></td>
    </c:if>

关于java - 如何在JSP..中进行分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31410007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com