java Spring Data 网页分页“页面”参数不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15578555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data web pagination "page" parameter not working
提问by Biju Kunjummen
I'm trying to get Spring Data's web pagination working. It's described here:
我试图让 Spring Data 的网页分页工作。它在这里描述:
Here's my Java (Spring Web MVC @Controller handler method):
这是我的 Java(Spring Web MVC @Controller 处理程序方法):
@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:
这是我的 Spring 配置:
<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
当我实际尝试使用page
和page.size
参数控制分页时,后者工作得很好,但前者不行。例如,如果我打
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.
所以我知道参数解析器正在启动,但不确定为什么它不解析页码。我尝试了一堆其他参数名称(例如 page.number、pageNumber、page.num 等),但没有一个起作用。
Is this working for anybody else?
这对其他人有用吗?
回答by Biju Kunjummen
the page parameter is actually a bit non-intuitive - page.page
and not page
, changing to page.page
should get things to work.
page 参数实际上有点不直观 -page.page
而不是page
,更改为page.page
应该可以使事情正常工作。
回答by Panthro
Looking into the PageableArgumentResolver
I found that prefix
and separator
are configurable, so you can configure the class not to have it.
查看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 = ".";
in my @Configuration class I did something different to achieve using page
, size
,sort
and sortDir
as default.
在我@Configuration类我做了不同的使用来实现page
,size
,sort
和sortDir
为默认值。
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
PageableArgumentResolver resolver = new PageableArgumentResolver();
resolver.setPrefix("");
resolver.setSeparator("");
argumentResolvers.add(new ServletWebArgumentResolverAdapter(resolver));
}
Now this works
现在这有效
http://:8080/myapp/list?page=14&size=42
http://:8080/myapp/list?page=14&size=42
回答by Nik
You can override parameters if you want by doing:
如果需要,您可以通过执行以下操作来覆盖参数:
<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>