July 04, 2015

Spring MVC: PagedListHolder



Spring 3 MVC framework provides supporting classes/annotations that can prove to be real "gems" during development of your web application. I'll be covering a few of them, the first being "PagedListHolder".


PagedListHolder


Spring Framework comes with a bean called org.springframework.bean.support. PagedListHolderAs per documentation, PagedListHolder is a simple state holder bean for handling lists of objects, separating them into pages. It has setter methods to indicate page size, page number.

For e.g in your specific Controller class you can add the following code snippet to create paginated data that can be accessed in a JSP or any other View.



 


String srchkey= request.getParameter("srchkey");
if (srchkey!= null) 
{
 PagedListHolder<Product> productList = new PagedListHolder<Product>(getProductList(srchkey)); 
productList.setPage(0);
        productList.setPageSize(4);        request.getSession().setAttribute("productList", productList); 
        return new ModelAndView("ListProducts", "productList", productList);    
}
else 
{ 
        String page = request.getParameter("page"); 
        PagedListHolder productList = (PagedListHolder)request.getSession().getAttribute("productList"); 

        if ("next".equals(page)) 
            productList.nextPage(); 

        else if ("previous".equals(page))
            productList.previousPage();
       
        return new ModelAndView("ListProducts", "productList", productList);    
}




References

http://stackoverflow.com/questions/2245035/how-to-implement-pagination-in-spring-mvc-3




No comments:

Post a Comment