The select statement results are stored in the queryResults variable. Here is the paginator code:
<c:set var="totalCount" scope="session" value="${queryResults.rowCount}"/>
<c:set var="perPage" scope="session" value="20"/>
<c:set var="totalPages" scope="session" value="${totalCount/perPage}"/>
<c:set var="pageIndex" scope="session" value="${param.start/perPage+1}"/>
<c:if test="${!empty param.start && param.start >(perPage-1) && param.start !=0 }">
<a href="?start=<c:out value="${param.start - perPage}"/>">Prev </a>
</c:if>
<c:forEach
var="boundaryStart"
varStatus="status"
begin="0"
end="${totalCount - 1}"
step="${perPage}">
<c:choose>
<c:when test="${status.count>0 && status.count != pageIndex}">
<a href="?start=<c:out value='${boundaryStart}'/>">
<c:out value="${status.count}"/> |
</a>
</c:when>
<c:otherwise>
<c:out value="${status.count}"/> |
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${empty param.start || param.start<(totalCount-perPage)}">
<a href="?start=<c:out value="${param.start + perPage}"/>">Next </a>
</c:if>
The code makes heavy use of the count property of the varStatus attribute.
This attribute keeps count of the current round of the iteration.
Thank you for nice and simple solution. I am looking for doing something similar but using a "sliding window" for pagination... that is:
ReplyDeleteEx: total pages = 10 with 25 items per page
1 2 3 4 5 Next> ... When user clicks Next>, then it shows: <Prev 6 7 8 9 10
How can I accomplish this? What needs to be modified in logic presented? Please advise. Thank you again.
Hi , you can insert condition in forEach statement :
Deletec:if test="${status.count >= (pageIndex-5) && status.count <= (pageIndex+5)}"
I enjoyed the implementation showing how I would do it with style
ReplyDeletepag.jsp?pag=1&rows=5
Thanks for sharing. I'm iterating a list of objects, so I may try to modify your example since you are iterating through query results.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi i have a problem in paging in jsp...
ReplyDeletei have list of objects in jsp page like:::List userlist=(List)request.getAttribute("users"); this is my list objects. after i have no idea about how to paging objects in jsp page....please help ASAP....
and one more thing is objects are userbean objects like pojo class(UserBean)
ReplyDelete