February 28, 2021
Hot Topics:

A Pagination Technique Using Spring

  • By Vlad Kofman
  • Send Email »
  • More Articles »

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>


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

  • Spring-Pagination

    For Further Reading

  • Spring Framework's PagedListHolder
  • JSP Pagination with Spring
  • "Implementing Search Result Pagination in a Web Application"

    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.

  • Page 3 of 3

    This article was originally published on July 21, 2009

    Enterprise Development Update

    Don't miss an article. Subscribe to our newsletter below.

    Thanks for your registration, follow us on our social networks to keep up-to-date