为 JPA Pageable Object 设置默认页面大小 [英] Set default page size for JPA Pageable Object

查看:37
本文介绍了为 JPA Pageable Object 设置默认页面大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PagingandSorting Repository,它有一个接受可分页对象的方法.我还有一个通过 URL 接受可分页对象的控制器.

I have a PagingandSorting Repository which has a method that accecpts a pageable object. I also have a controller that accepts a pageable object through the URL.

我的用例是,如果用户在 URL 中指定了页面大小参数,我必须为可分页对象采用该值.如果他不提及取默认值 50.

My use case is that, if a user specifies a page size parameter in the URL i must take that value for the pageable object. If he does not mention take a default value of 50.

但是可分页对象现在默认为 20.

But the pageable object defaults to 20 right now.

任何建议都会有所帮助

推荐答案

如果您正在谈论 Spring Data PagingAndSortingRepository,您可以通过在 Controller 方法上使用 @PageableDefault 来设置默认页面大小,如如下:

If you are talking about a Spring Data PagingAndSortingRepository you can set the default page size by using the @PageableDefault on a Controller method as follows:

public String listClients(@ModelAttribute FilterForm form, Model model, WebRequest request, @PageableDefault(sort = { "surname",
            "forename", "address.town" }, value = 50) Pageable pageable) {

    }

或者,您可以在 Spring 配置中使用以下内容配置全局默认值,如下所示,在 XML 和 Java 配置中.

Or you can configure a global default using the following in your Spring config as shown below in both XML and Java config.

请注意,较新版本的 Spring Data 使用基于零的页面索引,而旧版本使用 1 作为第一页.如果您的 UI 分页库需要 1 作为第一页,那么您可以将 oneIndexedParameters 属性设置为 true:

Note that newer versions of Spring Data use zero based page indexing while older versions used 1 for the first page. If your UI paging library expects 1 as first page then you can set the oneIndexedParameters property to true:

配置是否公开和假设基于 1 的页码索引请求参数.默认为 false,表示页码为 0在请求中等于第一页.如果设置为 true,则页面请求中的数字 1 将被视为第一页.

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.

参数:oneIndexedParameters - 要设置的 oneIndexedParameters

Parameters: oneIndexedParameters - the oneIndexedParameters to set

  • public void setFallbackPageable(Pageable fallbackPageable)
  • 将 Pageable 配置为在没有 PageableDefault 或PageableDefaults(后者仅在传统模式下支持)可以是在要解析的方法参数处找到.如果将此设置为空,请注意,您的控制器方法将在如果在请求中找不到可分页数据.请注意,这样做将要求您提供页面和大小参数与请求,因为任何参数都没有默认值可用.

    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.

    参数: fallbackPageable - 用作一般的 Pageable回退.

    Parameters: fallbackPageable - the Pageable to be used as general fallback.

    在 XML 中,如下所示:

    In XML this looks like the following then:

    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
                <property name="oneIndexedParameters" value="true"/>
                <property name="fallbackPageable">
                    <bean class="org.springframework.data.domain.PageRequest">
                        <constructor-arg name="page" value="1" />
                        <constructor-arg name="size" value="10" />
                    </bean>
                </property>
            </bean>
        </mvc:argument-resolvers>
    </mvc:annotation-driven>
    

    在 Java Config 中,如下所示:

    In Java Config this looks like the below:

    @Configuration
    public class WebConfig extends WebMvcConfigurerAdapter {
    
    
        @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
            PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
            resolver.setOneIndexedParameters(true);
            resolver.setFallbackPageable(new PageRequest(1, 20));
            argumentResolvers.add(resolver);
            super.addArgumentResolvers(argumentResolvers);
        }
    }
    

    这篇关于为 JPA Pageable Object 设置默认页面大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆