How to convert a List of enity Object to Page Object in Spring Mvc Jpa?

后端 未结 3 933
傲寒
傲寒 2021-02-01 01:31

I have a List of entities. How do I convert it to Page Object using Spring MVC 4 and Spring Data JPA?

相关标签:
3条回答
  • 2021-02-01 02:00

    There is a Page implementation for that:

    final Page<Something> page = new PageImpl<>(theListOfSomething);
    
    0 讨论(0)
  • 2021-02-01 02:14

    There is one more Constructor :

    Page<FOO> page = new PageImpl<>(listOfsomething, pageable, listOfsomething.size());
    
    0 讨论(0)
  • 2021-02-01 02:24

    I think you will need to fetch the correct page content as well.

    
    PageRequest pageRequest = PageRequest.of(offset, limit);
    
    List<Product> products = getProducts();
    
    int total = products.size();
    int start = toIntExact(pageRequest.getOffset());
    int end = Math.min((start + pageRequest.getPageSize()), total);
    
    List<Product> output = new ArrayList<>();
    
    if (start <= end) {
        output = products.subList(start, end);
    }
    
    return new PageImpl<>(
        output,
        pageRequest,
        total
    );
    
    0 讨论(0)
提交回复
热议问题