java ee - How To Do Pagination In JSP..? -
this question has answer here:
- resultset pagination 5 answers
please not down rate question, because i'm new jsp/javaee..
i looked many tutorials jstl, pagination, cannot proper idea pagination.. there esay way perform pagination..?
in program, retrieve records of search results database using java class , put them arraylist , returns arraylist servlet. assigns received arraylist object application attribute this
request.setattribute("results", results_list);
then using request dispatcher, i'm loading result showing jsp page this.
requestdispatcher rd = request.getrequestdispatcher("searchresults.jsp"); rd.forward(request, response);
so tell me next step pagination search reslts..
in servlet, first getting value of ‘page’ parameter , storing in ‘page’ variable. wanted display 5 (5) records per page passing argument viewallemployees(offset, 5). storing 3 attributes in request scope , forwarding request jsp page;
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 :
<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 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 not display link 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>
Comments
Post a Comment