How to implement pagination in Spring MVC 3

Is there a ready-made, easy-to-use standard pagination component / lib tag or sample code available for pagination in Spring MVC?

+56
java spring spring-mvc servlets pagination
Feb 11 2018-10-11
source share
6 answers

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

See JPetstore for examples for some examples, for example. 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); } } 
+86
Feb 11 '10 at 15:45
source share

I was looking for a way to do this too, but did not find any standard component or taglib. I think, mainly because paging can become very specific, since you need to get paging data from the database already (if you use Hibernate, you can easily do this using the API criteria). 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 execute the request at http://domain/app/test.html?id=10&page=1&results=30&sortOrder=asc , you will get a pager object in your request.

+11
Feb 11 '10 at 15:15
source share

No one comes to mind, and Google also does not disclose any specific components for this (although it gives fairly specific examples and tips). But theoretically, a few buttons and one (or two) query parameters are more than enough. Then let SQL / DB do its job. I posted an answer to a similar question in the context of JSP / Servlet / DAO before here .

This basically boils down to passing firstrow (the index of the first row displayed on the page) around the query parameter, and having two buttons / links in the form of pagination, which in / reduces firstrow using rowcount (the number of rows displayed immediately on page) in combination with an SQL query that returns a subset of the results using under each LIMIT , OFFSET statements or subqueries or specific functions depending on the database in question. See the above answer for detailed code examples and SQL queries.

+4
Feb 11 '10 at 15:16
source share

Have you ever heard of the Spring Data JPA project? There is a nice flexible solution using the Pagable interface. I found this to be the easiest way to get clean without pagination patterns. For more information, see the Spring JPA Data Home Page .

+4
Feb 08 '13 at 14:57
source share

Here's a link to Spring Data JPA reference documents , where they have a very clean pagination approach.

+3
Mar 22 '13 at 19:47
source share

I published an open source Java library focused on paging with the spring framework some time ago.

Although he was not very successful, perhaps someone might be interested in his attempt.

There are examples for use with

The examples on the Internet are somewhat outdated; it is better to download the jdal-samples file from sourceforge .

+2
Jul 10 '13 at 2:04 on
source share



All Articles