Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring PageableArgumentResolver deprecated, how to use PageableHandlerMethodArgumentResolver?

This is my current PageableResolver:

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    PageableArgumentResolver resolver = new PageableArgumentResolver();
    resolver.setFallbackPageable(new PageRequest(1, 5));
    argumentResolvers.add(new ServletWebArgumentResolverAdapter(resolver));
}

But the PageableArgumentResolver is deprecated, and linked to the PageableHandlerMethodArgumentResolver, which i suppose we can register without an Adapter, like this:

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
    resolver.setFallbackPageable(new PageRequest(0, 5));
    argumentResolvers.add(resolver);
}

First of all the implementation of new class PageableHandlerMethodArgumentResolver is a little different, since new PageRequest(1, 5) refers to the 2. page so i had to use new PageRequest(0, 5) in order it displays the first page.

But i still have problems with this new object. First load of the pagination bar is perfect when I call the URL without parameters (neither page.page nor page.size). Then when i want to move on the pagination bar, we are calling the same url with 2 parameters (eg. page.page=3&page.size=5), there is still displayed the first page. I think this new Resolver needs more than just these 2 parameters, so that it doesn't activate the fallback condition. Do you have any idea how to work with this PageableHandlerMethodArgumentResolver?

like image 282
akcasoy Avatar asked Nov 03 '13 19:11

akcasoy


2 Answers

You can change the parameter names, using this methods: setPageParameterName, setSizeParameterName.

Set this setOneIndexedParameters to true if your initial page is 1.

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
    resolver.setPageParameterName("page.page");
    resolver.setSizeParameterName("page.size");
    resolver.setOneIndexedParameters(true);
    argumentResolvers.add(resolver);
    super.addArgumentResolvers(argumentResolvers);
}
like image 113
user3799232 Avatar answered Dec 26 '22 15:12

user3799232


I have just found the answer in the Implementation of this class. We now need 2 changes for this new class:

  1. The parameters do not have the prefix "page" anymore. "..?page=2&size=5" works.
  2. Since the page 1 refers to the 2. page now, we have to redefine our parameters in the pagination.jsp: ..?page=${item.number-1} instead of ..?page=${item.number}
like image 26
akcasoy Avatar answered Dec 26 '22 14:12

akcasoy