如何从第1页开始配置Spring Boot分页,而不是0 [英] How to configure Spring boot pagination starting from page 1, not 0

查看:450
本文介绍了如何从第1页开始配置Spring Boot分页,而不是0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

boot(1.4.0)用于分页的可分页".它工作正常,没有任何问题.但是默认情况下,页面值从"0"开始,但是在前端,页面值从"1"开始.那么,有什么标准的方法可以增加值,而不是手动增加代码中的页码?

boot(1.4.0) "Pageable" for pagination.It works fine without any issue.But by default the page value starts from "0" but in the front-end the page value starts from "1". So is there any standard approach to increment value instead of manually increment the page number inside the code?

public Page<Device> find(DeviceFindCommand deviceFindCommand, Pageable pageable){
//page = 0 //Actual is 0, Expected increment by 1. 
}

任何帮助都应该是有意义的.

Any help should be appreciable.

实施 Alan 回答后,遇到以下问题,

After implementing Alan answers having the following issues,

1)仍然可以访问返回第一页的零页面(我不知道这是不是问题,但我想获得更好的清晰度).

1) Still i am able to access zero page which returns the first page(I don't know this is issue or not but i want to get a better clarity).

http://localhost:8180/api/v1/books /?page = 3& size = 2

回复

{
    "content": [
{
  "id": "57da9eadbee83fb037a66029",
  .
  .
  .
}{
.
.
.
}
],
    "last": false,
    "totalElements": 5,
    "totalPages": 3,
    "size": 2,
    "number": 2, //strange always getting 1 less than page number.
    "sort": null,
    "first": true,
    "numberOfElements": 2
}

2)数字":2 在响应中总是比页面数少一个.它应该返回当前页面索引.

2) "number": 2, in the response always getting one less than the page number.It should return the current page index.

推荐答案

如果您使用的是Spring Boot 2.X,则可以从WebMvcConfigurerAdapter切换到应用程序属性.有一组属性可配置可分页:

If you are using Spring Boot 2.X you could switch from WebMvcConfigurerAdapter to application properties. There is a set of properties to configure pageable:

# DATA WEB (SpringDataWebProperties)
spring.data.web.pageable.default-page-size=20 # Default page size.
spring.data.web.pageable.max-page-size=2000 # Maximum page size to be accepted.
spring.data.web.pageable.one-indexed-parameters=false # Whether to expose and assume 1-based page number indexes.
spring.data.web.pageable.page-parameter=page # Page index parameter name.
spring.data.web.pageable.prefix= # General prefix to be prepended to the page number and page size parameters.
spring.data.web.pageable.qualifier-delimiter=_ # Delimiter to be used between the qualifier and the actual page number and size properties.
spring.data.web.pageable.size-parameter=size # Page size parameter name.
spring.data.web.sort.sort-parameter=sort # Sort parameter name.

但是请记住,即使更改one-indexed-parameter,页面响应(PageImpl类)也将返回带有从零开始的页码的结果.可能有点误导.

But please remember that even if you change the one-indexed-parameter the page response (PageImpl class) will return results with zero-based page number. It could be a little misleading.

这篇关于如何从第1页开始配置Spring Boot分页,而不是0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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