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?
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);
}
I have just found the answer in the Implementation of this class. We now need 2 changes for this new class:
..?page=2&size=5
" works...?page=${item.number-1}
instead of ..?page=${item.number}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With