gpt4 book ai didi

java - 从 List 列表对象获取 Page 页面对象

转载 作者:太空宇宙 更新时间:2023-11-04 14:29:20 25 4
gpt4 key购买 nike

我有一个具体的实体列表。我的模型将 resieves 作为属性 Page<T> page对象,因为我有分页。有什么办法可以“转换”List<T> listPage<T> page ,考虑到我想使用的参数: int page, int size

我的 Controller :

@RequestMapping(value = "/symptoms", method = RequestMethod.GET)
public String symptomPage(
@RequestParam(required = false, value = "rows") Integer rows,
@RequestParam(required = false, value = "page") Integer tabNumber,
@RequestParam(required = false, value = "sort") String order,
@RequestParam(required = false, value = "isSorted") Boolean isSorted,
@RequestParam(required = false, value = "search") String search,
Model model) {
Page<Symptom> page = null;
if (rows == null && tabNumber == null) {
page = symptomService.findSymptomsPages(0, 10);
}
else if(!isSorted){
page = symptomService.findSymptomsPages(tabNumber - 1, rows, search);
} else {
page = symptomService.findSortedSymptomPages(tabNumber - 1, rows, order, search);
}
int currentPage = page.getNumber() + 1;
int first = Math.max(1, currentPage - 3);
int last = Math.min(currentPage + 3, page.getTotalPages());

model.addAttribute("symptom", new Symptom());
model.addAttribute("symptomPage", page);
model.addAttribute("currentPage", currentPage);
model.addAttribute("first", first);
model.addAttribute("last", last);
return "symptoms";
}

最佳答案

定义要用于页面的列表范围。并获取所需的子列表:

class Page<T> {
private final List<T> list;
private final pageSize;

// ... consturcotor

int getTabCount() {
return (list.size() + pageSize - 1) / pageSize;
}

List<T> getTabContent(int tab) {
int fromIndex = tab * size;
int toIndex = (tab + 1) * size;
if (fromIndex < 0)
fromIndex = 0;
if (toIndex > list.size())
toIndex = list.size();
if (fromIndex > toIndex)
toIndex = fromIndex;
return list.subList(fromIndex, toIndex);
}
}

关于java - 从 List<T> 列表对象获取 Page<T> 页面对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26330297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com