Java 如何在JSP中进行分页..?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31410007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Do Pagination In JSP..?
提问by Indunil Girihagama
Please do not down rate my question, because i'm new for JSP/JavaEE..
请不要低估我的问题,因为我是 JSP/JavaEE 的新手。
I looked so many tutorials about JSTL, Pagination, but i cannot get a proper idea to do Pagination.. Is there an esay way to perform Pagination..?
我看了很多关于 JSTL、Pagination 的教程,但我无法正确地做 Pagination .. 有没有一种 Esay 方法来执行 Pagination ..?
In my program, I retrieve records of search results from database using a java class and put them into a ArrayList and returns ArrayList to Servlet. Then assigns the received ArrayList object into an application attribute like this
在我的程序中,我使用 java 类从数据库中检索搜索结果的记录并将它们放入一个 ArrayList 并将 ArrayList 返回给 Servlet。然后将接收到的 ArrayList 对象分配给这样的应用程序属性
request.setAttribute("Results", results_List);
then using request dispatcher, i'm loading my result showing jsp page like this.
然后使用请求调度程序,我正在加载我的结果,显示像这样的 jsp 页面。
RequestDispatcher rd = request.getRequestDispatcher("searchresults.jsp");
rd.forward(request, response);
so tell me the next step to do pagination my search reslts..
所以告诉我下一步做分页我的搜索结果..
采纳答案by ozgur
In Servlet, we are first getting the value of ‘page' parameter and storing it in ‘page' variable. We wanted to display five (5) records per page which we are passing as an argument to viewAllEmployees(offset, 5). We are storing three attributes in the request scope and forwarding the request to a JSP page;
在 Servlet 中,我们首先获取 'page' 参数的值并将其存储在 'page' 变量中。我们希望每页显示五 (5) 条记录,我们将这些记录作为参数传递给 viewAllEmployees(offset, 5)。我们在请求范围内存储三个属性并将请求转发到一个 JSP 页面;
Servlet Class:
服务类:
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 File :
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>