A Pagination Technique Using Spring
The View Layer
On the JSP layer, you need to read this object and have a way to show page links and its contents. The hidden variable p controls the page the user is viewing, and the <c:forEach loop traverses the result and displays its contents.
<%@ include file="/jsp/includes.jsp" %> <%@ taglib prefix="tg" tagdir="/WEB-INF/tags" %> <%-- // use our pagedListHolder --%> <jsp:useBean id="pagedListHolder" scope="request"
type="org.springframework.beans.support.PagedListHolder"/> <%-- // create link for pages, "~" will be replaced
later on with the proper page number --%> <c:url value="/paging.do" var="pagedLink"> <c:param name="action" value="list"/> <c:param name="p" value="~"/> </c:url> <%-- // load our paging tag, pass pagedListHolder and the link --%> <tg:paging pagedListHolder="${pagedListHolder}" pagedLink="${pagedLink}"/> <%-- // show only current page worth of data --%> <table width="200px" border="1"> <tr> <th width="20px">No.</th> <th>Random Number</th> </tr> <c:forEach items="${pagedListHolder.pageList}" var="item"> <tr> <td>${item.key}</td> <td style="color:blue;font-weight:bold;text-align:right">${item.data}</td> </tr> </c:forEach> </table> <%-- // load our paging tag, pass pagedListHolder and the link --%> <tg:paging pagedListHolder="${pagedListHolder}" pagedLink="${pagedLink}"/>
Note that the actual page links display logic is defined in the JSP tag "<tg:paging>"(/WEB-INF/tags/paging.tag). The pagedListHolder and current page are passed to it as parameters. The tag then correctly displays page links, including arrows and gaps in pages if the number exceeds specific display limits.
<tg:paging pagedListHolder="${pagedListHolder}" pagedLink="${pagedLink}"/>
Here is the source for the paging tag, which should be included in the WEB-INFtags folder.
<%@ tag import="org.springframework.util.StringUtils" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ attribute name="pagedListHolder" required="true" type="org.springframework.beans.support.PagedListHolder" %>
<%@ attribute name="pagedLink" required="true" type="java.lang.String" %>
<link href="css/pagination.css" rel="stylesheet" type="text/css"/>
<c:if test="${pagedListHolder.pageCount > 1}">
<c:if test="${!pagedListHolder.firstPage}">
<span class="pagingItem"><a href="<%= StringUtils.replace(pagedLink,
"~", String.valueOf(pagedListHolder.getPage()-1)) %>"><</a></span>
</c:if>
<c:if test="${pagedListHolder.firstLinkedPage > 0}">
<span class="pagingItem"><a href="<%= StringUtils.replace(pagedLink, "~", "0") %>">1</a></span>
</c:if>
<c:if test="${pagedListHolder.firstLinkedPage > 1}">
<span class="pagingDots">...</span>
</c:if>
<c:forEach begin="${pagedListHolder.firstLinkedPage}" end="${pagedListHolder.lastLinkedPage}" var="i">
<c:choose>
<c:when test="${pagedListHolder.page == i}">
<span class="pagingItemCurrent">${i+1}</span>
</c:when>
<c:otherwise>
<span class="pagingItem"><a href="<%= StringUtils.replace(pagedLink,
"~", String.valueOf(jspContext.getAttribute("i"))) %>">${i+1}</a></span>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${pagedListHolder.lastLinkedPage < pagedListHolder.pageCount - 2}">
<span class="pagingDots">...</span>
</c:if>
<c:if test="${pagedListHolder.lastLinkedPage < pagedListHolder.pageCount - 1}">
<span class="pagingItem"><a href="<%= StringUtils.replace(pagedLink,
"~", String.valueOf(pagedListHolder.getPageCount()-1)) %>">${pagedListHolder.pageCount}</a></span>
</c:if>
<c:if test="${!pagedListHolder.lastPage}">
<span class="pagingItem"><a href="<%= StringUtils.replace(pagedLink,
"~", String.valueOf(pagedListHolder.getPage()+1)) %>">></a></span>
</c:if>
</c:if>
Conclusion
This article discussed a pagination mechanism based on the Spring Framework. The implementation is designed for a Java application's server layer and assumes JSP client technology. If your web or portlet project is already using Spring, adding pagination should be straightforward. The article also discussed different approaches to the pagination logic and the best layer in which to implement it.
Special thanks to Joe Zhao for providing some of the sample code for this article and guiding me in the implementation details.
Code Download
For Further Reading
About the Author
Vlad Kofman works on enterprise-scale projects for major Wall Street firms. He has also worked on defense contracts for the U.S. government. His main interests are web programming methodologies, UI patterns, and SOA.
More for Developers
On the Codeguru Forums
Visit the Forums »eBooks, Webcasts, Whitepapers and More
Tutorial: Light Up Your Windows 7 App with Jump Lists, Libraries, and Microsoft Platform Ready
Custom Walkthrough: Create an Azure-integrated Windows 7 Phone App
Article: Thuzi, Outback and Azure--Tapping the Power of Facebook and the Cloud
Article: Windows 7 Light-Up--Make Your Applications Shine on Windows 7
Article: Submitting Your Windows Phone 7 Application to the Windows Marketplace



Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.