December 19, 2014
Hot Topics:

A Pagination Technique Using Spring

  • July 21, 2009
  • By Vlad Kofman
  • Send Email »
  • More Articles »

Java Spring implementation

Assuming your application returns small- to medium-sized result sets every time, then implementing the Spring Framework on the application server layer is a good approach. You can easily combine this approach with memory-caching to save database resources and reduce network traffic.

The logic to do Spring pagination will reside mainly on the server, but requires a small UI control on the client. The client-side control handles the look and feel for pagination, and exposes links that let the server know which portion of the total result set to return next (a.k.a., page navigation).

Here is an example of the look-and-feel CSS (download project for full source)

The Client-Side Look and Feel

.pagingItem {
font-weight: normal;
text-decoration: none;
color: #747474;
margin: 0 2px;
padding: 0 2px;
background-color: #eeeeee;
border: 1px solid #bababa;
font-size: 0.9em;
line-height: 1.5em;
}
.pagingItemCurrent {
padding: 0 2px;
margin: 0 2px;
font-weight: normal;
color: #FFFFFF;
background-color: #bfbfbf;
border: 1px solid #bfbfbf;
font-size: 0.9em;
}

This will render something like this:

But you can easily change the CSS to any preferred look:

The Controller

The Spring Framework comes with a bean called org.springframework.beans.support.PagedListHolder,. This class will do all the heavy lifting for the pagination logic implementation. This bean needs to be populated with a List; and setter methods are available to indicate page size and page number. The bean will do the rest. You set it in the controller, which then returns a ModelAndView object to the client layer.

mav.addObject("pagedListHolder", pagedListHolder);

Here is a description of this bean from the Spring API:

"PagedListHolder is a simple state holder for handling lists of objects, separating them into pages. Page numbering starts with 0. "

This is mainly targeted for usage in web UIs. Typically, an instance will be instantiated with a list of beans, put into the session, and exported as a model. The properties can all be set/get programmatically, but the most common way will be data binding (i.e., populating the bean from request parameters). The getters will mainly be used by the view.

The controller supports sorting the underlying list via a SortDefinition implementation, available as property "sort." By default, a MutableSortDefinition instance will be used, toggling the ascending value when you set the same property again.

The data binding names have to be called "pageSize" and "sort.ascending," as expected by BeanWrapper. Note that the names and the nesting syntax match the respective JSTL EL expressions, such as "myModelAttr.pageSize" and "myModelAttr.sort.ascending."

Here is the code portion from the source that enables pagination. In this example, the ItemDao simply returns a list of random numbers, but a real-world application would be similar, although it would get its data from a database. (See the complete source for more details.)

public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
springAppContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
ModelAndView mav = new ModelAndView("paging");
// get data
ItemDao dao = (ItemDao) springAppContext.getBean("itemDao");
List searchResults = dao.getAllItems();
// initialize PagedListHolder with our list, set current page defaulted to 0, and pass it to the view
PagedListHolder pagedListHolder = new PagedListHolder(searchResults);
int page = ServletRequestUtils.getIntParameter(request, "p", 0);
pagedListHolder.setPage(page);
int pageSize = 10;
pagedListHolder.setPageSize(pageSize);
mav.addObject("pagedListHolder", pagedListHolder);
return mav;
}




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

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

Sitemap | Contact Us

Rocket Fuel