【发布时间】:2021-08-17 10:23:44
【问题描述】:
我正在尝试在我的网站上进行分页。如果我在“/”控制器中选择任何起始页,它可以工作,但是当我转到其他页面时它会损坏。转换过程中出现错误“HTTP Status 500 – Internal Server Error”
控制器`
@GetMapping("/")
public String home(Model model) {
return findPaginated(1, model);
}
@GetMapping("/{pageNO}")
public String findPaginated(@PathVariable (value = "pageNo") int pageNo, Model model) {
int pageSize=6;
Page<Recipe> page = recipeService.findPaginated(pageNo,pageSize);
List<Recipe> listRecipes = page.getContent();
model.addAttribute("currentPage", pageNo);
model.addAttribute("totalPages", page.getTotalPages());
model.addAttribute("totalItems", page.getTotalElements());
model.addAttribute("listRecipes", listRecipes);
return "home";
}
`
ServiceImp
@Service
public class RecipeServiceImpl implements RecipeService {
@Autowired
RecipeRepository recipeRepository;
@Override
public Page<Recipe> findPaginated(int pageNo, int pageSize)
{
Pageable pageable = PageRequest.of(pageNo-1, pageSize);
return this.recipeRepository.findAll(pageable);
}
}
home.html
<div th:if = "${totalPages>1}">
<div class="row col-sm-10">
<div class="col-sm-2">total records: [[${totalItems}]]</div>
<div class = "col-sm-1">
<span th:each="i: ${#numbers.sequence(1, totalPages)}">
<a th:if="${currentPage != i}" th:href="@{'/' + ${i}}">[[${i}]]</a>
<span th:unless="${currentPage != i}">[[${i}]]</span>
</span>
</div>
<div class = "col-sm-1">
<a th:if="${currentPage < totalPages}" th:href="@{'/' + ${currentPage + 1}}">Next</a>
<span th:unless="${currentPage < totalPages}">Next</span>
</div>
<div class="col-sm-1">
<a th:if="${currentPage < totalPages}" th:href="@{'/' + ${totalPages}}">Last</a>
<span th:unless="${currentPage < totalPages}">Last</span>
</div>
</div>
</div>
【问题讨论】:
-
提供应用程序日志以便更好地理解
-
程序代码说 "@GetMapping("/{pageNO}") ...@PathVariable (value = "pageNo")" ... pageNO 和 pageNo 不应该匹配吗?跨度>
标签: java html spring-boot pagination controller