We're moving Spring Solr onto the page in our NixMash Spring app, so the first thing I wanted to do was demonstrate paging with Spring Solr. Notice the nice Bootstrap pagination piece below? Complete with disabled “next” and “first/last”, highlighted current page and incrementing page elements. Nice! We're going to create it with Spring MVC, a Spring PagedListHolder object and Thymeleaf.
Solr is almost an irrelevant part of the mix, since we're really talking Spring JPA here. We'll start in the Service Implementation class where we see we're returning a List<Product> collection from the Solr Repository.
Since we're dealing with a List<?> (as opposed to a Page<?>) the Spring PlacedListHolder gives us added functionality for setting the current page and navigating through the list. We could also use a Page object and its similar methods, but for this example we'll stick with the PlacedListHolder.
Let's step through the Controller logic.
Now we'll create the Bootstrap Pagination control in Thymeleaf, which supplies all of the functionality needed to disable elements, increment page groups and loop through items based on the dimensions we passed from the Controller.
Before I sign off, a huge Hat Tip to Stefan Alexandru who laid all of this out in a 2014 blog post. Stefan used a Page object and has other good commentary on the process if you want to check it out.