Spring数据 - 分页 - 获取项目的页码

时间:2017-09-25 05:25:59

标签: spring-data paging

我在我的应用程序中使用分页。 这是我的代码:

final Pageable pageable = new PageRequest(page, this.getHistoryPageSize(), new Sort(Sort.Direction.ASC, "date"));
    final List<History> pagedHistory = historyRepository.findPagedHistoryByTenantAndDateBetween(tenant, start, end, pageable);

我现在的问题是,是否有可能获取某个项目的页码,以便我可以显示此页面。

1 个答案:

答案 0 :(得分:0)

使用getPageNumber()的方法Pageable

int pageNumber = pageable.getPageNumber();

示例:

@GetMapping("/")
public String users(
        @RequestParam(value = "q", required = false) String query,
        Model model,
        @PageableDefault(size = PAGE_SIZE, sort = SORT, direction = ASC) Pageable pageable
) {
    Page<User> users = repo.findByNameAndEmail((query != null) ? query : "", pageable);
    model.addAttribute("users", users);
    model.addAttribute("query", query);

    model.addAttribute("totalPages", users.getTotalPages());
    model.addAttribute("current", pageable.getPageNumber());
    model.addAttribute("previous", pageable.previousOrFirst().getPageNumber());
    model.addAttribute("next", pageable.next().getPageNumber());

    return "users";
}

source code