Java 我试图在我的网站上进行分页,但当我转到其他页面时,它会被破坏
我正在尝试在我的网站上进行分页。如果我在“/”控制器中选择任何起始页,它都会工作,但当我转到其他页时,它会损坏。“HTTP状态500–内部服务器错误”转换期间发生错误 控制器`Java 我试图在我的网站上进行分页,但当我转到其他页面时,它会被破坏,java,html,spring-boot,pagination,controller,Java,Html,Spring Boot,Pagination,Controller,我正在尝试在我的网站上进行分页。如果我在“/”控制器中选择任何起始页,它都会工作,但当我转到其他页时,它会损坏。“HTTP状态500–内部服务器错误”转换期间发生错误 控制器` @GetMapping("/") public String home(Model model) { return findPaginated(1, model); } @GetMapping("/{pageNO}")
@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";
}
@GetMapping(“/”)
公共字符串主页(模型){
返回FindPagined(1,型号);
}
@GetMapping(“/{pageNO}”)
公共字符串findPaginated(@PathVariable(value=“pageNo”)int pageNo,Model){
int pageSize=6;
Page Page=recipeService.FindPagined(页码、页面大小);
List listRecipes=page.getContent();
model.addAttribute(“当前页面”,页码);
addAttribute(“totalPages”,page.getTotalPages());
addAttribute(“totalItems”,page.getTotalElements());
model.addAttribute(“listRecipes”,listRecipes);
返回“家”;
}
`
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);
}
}
@服务
公共类RecipeServiceImpl实现RecipeService{
@自动连线
交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互交互;
@凌驾
已查找到的公共页面(int pageNo,int pageSize)
{
Pageable Pageable=PageRequest.of(第1页,页面大小);
返回此.recipereposition.findAll(可分页);
}
}
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>
记录总数:[${totalItems}]]
[${i}]]
下一个
最后
应用程序应在其日志中返回导致此错误的原因。例如,当内部抛出NullPointerException时,Spring应用程序将返回500。您也可以提供这个吗?提供应用程序日志以更好地理解程序代码中的“@GetMapping(“/{pageNO}”)…@PathVariable(value=“pageNO”)”。。。pageNO和pageNO不应该匹配吗?