如果您正在讨论 Spring Data PagingAndSortingRepository,可以使用Controller方法上的@PageableDefault设置默认页面大小,如下所示:
public String listClients(@ModelAttribute FilterForm form, Model model, WebRequest request, @PageableDefault(sort = { "surname",
"forename", "address.town" }, value = 50) Pageable pageable) {
}
或者,您可以在Spring配置中使用以下配置全局默认值,如下所示在XML和Java配置中.
请注意,较新版本的Spring Data使用基于零的页面索引,而旧版本使用1作为第一页.如果您的UI分页库期望1作为第一页,那么您可以将oneIndexedParameters属性设置为true:
> public void setOneIndexedParameters(boolean oneIndexedParameters)
Configures whether to expose and assume 1-based page number indexes in
the request parameters. Defaults to false, meaning a page number of 0
in the request equals the first page. If this is set to true, a page
number of 1 in the request will be considered the first page.
Parameters: oneIndexedParameters – the oneIndexedParameters to set
> public void setFallbackPageable(Pageable fallbackPageable)
Configures the Pageable to be used as fallback in case no PageableDefault or
PageableDefaults (the latter only supported in legacy mode) can be
found at the method parameter to be resolved. If you set this to null,
be aware that you controller methods will get null handed into them in
case no Pageable data can be found in the request. Note, that doing so
will require you supply bot the page and the size parameter with the
requests as there will be no default for any of the parameters
available.
Parameters: fallbackPageable – the Pageable to be used as general
fallback.
在XML中,这看起来如下:
在Java Config中,如下所示:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addArgumentResolvers(List argumentResolvers) {
PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
resolver.setOneIndexedParameters(true);
resolver.setFallbackPageable(new PageRequest(1, 20));
argumentResolvers.add(resolver);
super.addArgumentResolvers(argumentResolvers);
}
}