【问题标题】:i am trying to do pagination on my site, but it gets corrupted when i go to other pages我正在尝试在我的网站上进行分页,但是当我转到其他页面时它会损坏
【发布时间】: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>  &nbsp; &nbsp;
                    </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


【解决方案1】:

您的应用程序应在其日志中返回导致此错误的原因。例如,当内部抛出 NullPointerException 时,Spring 应用程序将返回 500。你也可以提供这个吗?

【讨论】:

    猜你喜欢
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2022-12-19
    • 2021-03-12
    • 2021-10-03
    • 1970-01-01
    相关资源
    最近更新 更多