pageable设置size_spring – 设置JPA Pageable Object的默认页面大小

如果您正在讨论 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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值