/**
* Constructor of {@code PageImpl}.
*
* @param content the content of this page, must not be {@literal null}.
* @param pageable the paging information, must not be {@literal null}.
* @param total the total amount of items available. The total might be adapted considering the length of the content
* given, if it is going to be the content of the last page. This is in place to mitigate inconsistencies.
*/
public PageImpl(List<T> content, Pageable pageable, long total)

content:当前页的记录。
total:所有记录数
也就是用PageImpl实现Page接口的时候,不能把数据全取回来放入content,得按分页的size放入。而最后一个参数需要记录的是所有数据的计数。

    public Page<ESIndexObject> getAllESIndex(Pageable pageable) {
String strQuery = "/_cat/indices?v&h=uuid,health,status,index,docsCount,storeSize,cds&s=cds:desc&format=json";
Request request = new Request("GET", strQuery);
try {
Response response = restClient.performRequest(request);
String responseBody = EntityUtils.toString(response.getEntity());
ObjectMapper mapper = new ObjectMapper();
      
List<ESIndexObject> list = mapper.readValue(responseBody,new TypeReference<List<ESIndexObject>>(){});
int totalElements = list.size();
if(pageable==null)
{
pageable = PageRequest.of(0,10);
}
int fromIndex = pageable.getPageSize()*pageable.getPageNumber();
int toIndex = pageable.getPageSize()*(pageable.getPageNumber()+1);
if(toIndex>totalElements) toIndex = totalElements;
       //如果list的内容取回后基本不变化,可以考虑放入类对象变量里或者其他缓存里,然后判断list是否可用,不用每次都去取list,适合取数的时候无法真分页只能伪分页情况。
//类似取mysql数据库的情况,因为数据取数本身支持分页,所以list的数据每次都取当前页就可以,但是需要先要计算所有记录数,然后传入totalElements变量
List<ESIndexObject> indexObjects = list.subList(fromIndex,toIndex);#获取当前页数据
Page<ESIndexObject> page = new PageImpl<>(indexObjects,pageable,totalElements);
return page;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
 

如何正确实现Page接口分页,用PageImpl 自定义分页的更多相关文章

  1. Django 分页组件替换自定义分页

    Django的分页器(paginator) 总之不太好用我们还是用自己的好一些 自定义分页器 分页实现源码 """ 自定义分页组件 """ ...

  2. [ Laravel 5.6 文档 ]laravel数据库操作分页(自定义分页实现和自定义分页样式)

    简介 在其他框架中,分页可能是件非常痛苦的事,Laravel 让这件事变得简单.易于上手.Laravel 的分页器与查询构建器和 Eloquent ORM 集成在一起,并开箱提供方便的.易于使用的.基 ...

  3. django上课笔记2-视图CBV-ORM补充-Django的自带分页-Django的自定义分页

    一.视图CBV 1.urls url(r'^login.html$', views.Login.as_view()), 2.views from django.views import View cl ...

  4. 修改DeDe标签Pagelist分页样式,自定义分页样式

    我们在用dede仿站的时候,调用文章列表页的分页时,我们会用到: {dede:pagelist listitem="info,index,end,pre,next,pageno" ...

  5. Django自定义分页、bottle、Flask

    一.使用django实现之定义分页 1.自定义分页在django模板语言中,通过a标签实现; 2.前段a标签使用<a href="/user_list/?page=1"> ...

  6. PHP+jQuery 列表分页类 ( 支持 url 分页 / ajax 分页 )

    /* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8.3.mi ...

  7. Cookie、Session和自定义分页

    一.cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响, ...

  8. 自定义分页及Cookie、Session机制

    分页 自定义分页 data = [] , ): tmp = {"id": i, "name": "alex-{}".format(i)} d ...

  9. 使用Spring JPA中Page、Pageable接口和Sort类完成分页排序

    显示时,有三个参数,前两个必填,第几页,一页多少个size,第三个参数默认可以不填. 但是发现这个方法已经过时了,通过查看它的源码发现,新方法为静态方法PageRequest of(page,size ...

随机推荐

  1. 3551: [ONTAK2010]Peaks加强版

    3551: [ONTAK2010]Peaks加强版 https://www.lydsy.com/JudgeOnline/problem.php?id=3551 分析: kruskal重构树 +  倍增 ...

  2. 使用Google Cloud Messaging (GCM),PHP 开发Android Push Notifications (安卓推送通知)

    什么是GCM? Google Cloud  Messaging (GCM) 是Google提供的一个服务,用来从服务端向安卓设备发送推送通知. GCM分为客户端和服务端开发. 这里我们只介绍服务端开发 ...

  3. HashMap在并发场景下踩过的坑

    本文来自网易云社区 作者:张伟 关于HashMap在并发场景下的问题有很多人,很多公司遇到过!也很多人总结过,我们很多时候都认为这样都坑距离自己很远,自己一定不会掉入这样都坑.可是我们随时都有就遇到了 ...

  4. Selenium(Python)PageObject页面对象

    使用PageObject页面对象的好处是, 当页面元素的位置发生改变时, 只需要去修改Xpath或者ID, 而不用去修改测试用例本身: 本次的思路是: 1.常用方法类 2.页面对象类 3.测试用例类 ...

  5. div布局方案整理

    实际项目开发过程中遇到页面 DIV 左右布局的需求:左侧 DIV 固定宽度,右侧 DIV 自适应宽度,填充满剩余页面,由此引申出本文的几种解决方案 1 左侧 DIV 设置 float 属性为 left ...

  6. TPO-13 C1 Understand the assignment in psychology course

    TPO-13 C1 Understand the assignment in psychology course 第 1 段 1.listen to a conversation between a ...

  7. 反片语 (Ananagrams,UVa 156)

    题目描述: #include <iostream> #include <string> #include <cctype> #include <vector& ...

  8. vim常用命令—撤销与反撤销

    命令模式下(即按ESC后的模式) u 撤销 Ctrl r (组合键) 反撤销<后悔撤销>

  9. MATLAB画图符号标注

    线型 说明 标记符 说明 颜色 说明 - 实线(默认) + 加号符 r 红色 -- 双划线 o 空心圆 g 绿色 : 虚线 * 星号 b 蓝色 :. 点划线 . 实心圆 c 青绿色 x 叉号符 m 洋 ...

  10. [leetcode-670-Maximum Swap]

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...