Reputation: 3228
I want to display a pagination link in this way: current page and 3 links forward and 3 links backward.
Obviously i don't want to display the links for negative pages or for pages after the last one.
So in my pagination.jsp i have the following:
param.pages
is the total number of pages
without the <c:if test="${page < param.pages} ">
stuff, i display the links correctly but with no check on pages after param.pages. With the c:if, i display nothing, I think it's because the test does not recognize page
<c:forEach begin="${currentPage + 1 }" end="${currentPage + 3}" var="page">
<c:if test="${page < param.pages} ">
<li><a
href="${pageContext.request.contextPath}/${param.currentURL }/<c:out value="${page }"/>">
<c:out value="${page }"/> </a></li>
</c:if>
</c:forEach>
Which is the best way to procede?
Upvotes: 0
Views: 147
Reputation: 3228
I finally found it, using only JSP tags, don't know if it's the best solution, but it works, i'll write it down if anyone need it.
In my pagination.jsp
<c:set var="maxLink" value="4"></c:set> // it's the number of links you want before and after the current one
// this part is to avoid to have negative values in the forEach cycle for the previous links
<c:set var="bottomLimit" value="${param.currentPage - maxLink }"></c:set>
<c:choose>
<c:when test="${bottomLimit <= 0 }">
<c:set var="begin" value="1"></c:set>
</c:when>
<c:otherwise>
<c:set var="begin" value="${bottomLimit }"></c:set>
</c:otherwise>
</c:choose>
<c:forEach begin="${begin }" end="${param.currentPage -1}"
var="pageBefore">
<li><a
href="${pageContext.request.contextPath}/${param.currentURL }/<c:out value="${pageBefore }"/>"><c:out
value="${pageBefore }" /> </a></li>
</c:forEach>
// link to the currentPage
<li class="active"><a
href="${pageContext.request.contextPath}/${param.currentURL }/${param.currentPage}">${param.currentPage }</a></li>
//Link to the next pages, that ends at the last page
<c:set var="end" value="${param.currentPage + maxLink }"></c:set>
<c:forEach begin="${param.currentPage + 1 }" end="${end}" var="page">
<c:if test="${page <= param.pages}">
<li><a
href="${pageContext.request.contextPath}/${param.currentURL }/<c:out value="${page }"/>"><c:out
value="${page }" /> </a></li>
</c:if>
</c:forEach>
Upvotes: 1