02 August 2010

Neo-vintage 2: Pagination in Struts... JSTL implementation

Many times I have needed to use an pagination for many data to be shown in my page, however there is no a good Tag implemented for Struts and not as JSF that has components for it..

There is a good encapsulation with the great library DisplayTag, but this one doesn't give too much freedom to create different things, thus this approach is more flexible.

Finally searching for the web I found a simple JSTL pagination, where you can give a collection of data and at level of presentation parse it and embedded with html and javascript we can get the final pagination that we wish.http://web.archive.org/web/20071213001753/http://www.ekcsoft.com/jstl/content/paginate/ (it is no longer alive).

It has a small bug when the collection is empty therefore the totalCount is zero.

It can be put inside Struts without problems, below can see code of it,:
<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>

1 comment :

Anonymous said...

Thhhhaannkkkk youuu

Blog Archive

Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.