6

I'm trying to get Spring Data's web pagination working. It's described here:

http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/repositories.html#web-pagination

Here's my Java (Spring Web MVC @Controller handler method):

@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(
    @PageableDefaults(value = 50, pageNumber = 0) Pageable pageable,
    Model model) {

    log.debug("Params: pageNumber={}, pageSize={}",
        pageable.getPageNumber(), pageable.getPageSize());

    ...
}

And here's my Spring configuration:

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="org.springframework.data.web.PageableArgumentResolver" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>

(It appears that the configuration above is the way to do this now; the configuration approach described in the link is deprecated.)

When I actually try to control the pagination using the page and page.size parameters, the latter works just fine, but the former doesn't. For example, if I hit

http://localhost:8080/myapp/list?page=14&page.size=42

the log output is

Params: pageNumber=0, pageSize=42

So I know that the argument resolver is kicking in, but not sure why it's not resolving the page number. I've tried a bunch of other param names (e.g. page.number, pageNumber, page.num, etc.) and none of them work.

Is this working for anybody else?

4

3 回答 3

7

page 参数实际上有点不直观 -page.page而不是page,更改为page.page应该可以让事情正常工作。

于 2013-03-22T21:18:56.890 回答
2

查看PageableArgumentResolver我发现prefix并且separator是​​可配置的,因此您可以将类配置为不具有它。

public class PageableArgumentResolver implements WebArgumentResolver {

    private static final Pageable DEFAULT_PAGE_REQUEST = new PageRequest(0, 10);
    private static final String DEFAULT_PREFIX = "page";
    private static final String DEFAULT_SEPARATOR = ".";

在我的@Configuration 类中,我使用,page和作为默认值做了一些不同的事情。sizesortsortDir

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    PageableArgumentResolver resolver =  new PageableArgumentResolver();
    resolver.setPrefix("");
    resolver.setSeparator("");
    argumentResolvers.add(new ServletWebArgumentResolverAdapter(resolver));
}

现在这有效

http://:8080/myapp/list?page=14&size=42

于 2014-11-06T17:29:28.383 回答
0

如果需要,您可以通过以下方式覆盖参数:

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean   class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
            <property name="oneIndexedParameters" value="true"></property>
            <property name="pageParameterName" value="page"></property>
            <property name="sizeParameterName" value="size"></property>
        </bean>
    </mvc:argument-resolvers>
</mvc:annotation-driven>
于 2015-05-14T21:30:17.543 回答