views:

1913

answers:

3

Hello, Is there any out-of-the-box, easy to implement, standard pagination component/tag-lib or code-sample available for pagination in Spring MVC?

Cheers

+2  A: 

I was looking for a way to do that, too, but didn't find any standard component or taglib. I think mainly because paging can become very specific since you need to retrieve your data with paging from the database already (if you are using Hibernate you can easily do that using the Criteria API). I came up with something like this:

public class Pager
{
    private int page;
    private int results;
    private String sortOrder;
    private String sortColumn;

    // Getters and setters
}

@Controller
public class StuffController
{
    @Autowired SomeEntityService someEntityService;

    @RequestMapping("/test.html", method = Method.GET)
    public void getStuffPaged(@RequestParam("id") String id, Pager pager, ModelMap mm)
    {
        mm.addAttribute("entities", someEntityService.get(id, pager));
    }
}

If you now request http://domain/app/test.html?id=10&page=1&results=30&sortOrder=asc you will get the pager Objet in your request.

Daff
I am not using hibernate, but its cool.. good one.. :)
Nachiket
A: 

No one comes to mind and Google also doesn't reveal any specific components for that (although it gives pretty much concrete examples and hints). But in theory just a bunch of buttons and one (or two) request parameters are more than sufficient. Then let the SQL/DB do its work. I've posted an answer to similar question in JSP/Servlet/DAO context before here.

It basically boils down to pass firstrow (index of first row to be displayed in a page) around as request parameter and having two buttons/links in the pagination form which in/decrements the firstrow with rowcount (amount of rows displayed at once in a page) in combination with a SQL query which returns a subset of the results with help of under each LIMIT, OFFSET clauses, or subselects, or specific functions, depending on the DB in question. See the aforelinked answer for detailed code examples and SQL queries.

BalusC
+12  A: 

Have a look at PagedListHolder and other classes from org.springframework.beans.support.

See the JPetstore in the samples for some examples, e.g. in SearchProductsController.java:

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String keyword = request.getParameter("keyword");
    if (keyword != null) {
        if (!StringUtils.hasLength(keyword)) {
            return new ModelAndView("Error", "message", "Please enter a keyword to search for, then press the search button.");
        }
        PagedListHolder productList = new PagedListHolder(this.petStore.searchProductList(keyword.toLowerCase()));
        productList.setPageSize(4);
        request.getSession().setAttribute("SearchProductsController_productList", productList);
        return new ModelAndView("SearchProducts", "productList", productList);
    }
    else {
        String page = request.getParameter("page");
        PagedListHolder productList = (PagedListHolder) request.getSession().getAttribute("SearchProductsController_productList");
        if (productList == null) {
            return new ModelAndView("Error", "message", "Your session has timed out. Please start over again.");
        }
        if ("next".equals(page)) {
            productList.nextPage();
        }
        else if ("previous".equals(page)) {
            productList.previousPage();
        }
        return new ModelAndView("SearchProducts", "productList", productList);
    }
}
Pascal Thivent
(+1) Hot damn, I had no idea that was there. I must've implemented that functionality at least 3 times, and there it was already...
skaffman
cool, By the way, can you please explain, how session is needed in example of SearchProductsController code? I went through the code unable to get use of it.
Nachiket
@Nachiket Once created, the `PagedListHolder` object is put in the Session, the pagination is actually done on an in memory object. Does this answer your question? If not, can you clarify?
Pascal Thivent