代码之家  ›  专栏  ›  技术社区  ›  Nguyen Hoang Vu

将分页与Spring Boot和Thymeleaf一起使用时出错

  •  0
  • Nguyen Hoang Vu  · 技术社区  · 7 年前

    我有以下代码来对控制器发送的数据进行分页:

        <div class="col-xs-12 col-md-6">
            <ul class="list-group">
                <li class="list-group-item" th:each="history:${batchExecuteHistory}"
                    th:if="${history.status == true}">
                    <button type="button" class="btn btn-success">Success</button> 
                    <span th:text="${history.timeEnd}"></span>
                    <span th:text="${history.rowInput}"></span>
                </li>
                <li class="list-group-item" th:each="history:${batchExecuteHistory}"
                    th:if="${history.status == false}">
                    <button type="button" class="btn btn-danger">Danger</button> 
                    <span th:text="${history.timeEnd}"></span> 
                    <span th:text="${history.errorContent}"></span>
                </li>
            </ul>
            <ul class="pagination">
                <li th:each='i : ${#numbers.sequence(0, nubmerOfPage.totalPages-1)}'
                    th:class="(${i}==${pnubmerOfPage.number})? 'active' : ''"
                    style="display: inline"><span th:if='${i}==${nubmerOfPage.number}'
                    th:text='${i+1}'>1</span> <a th:if='${i}!=${nubmerOfPage.number}'
                    th:href="@{${url}(nubmerOfPage=${i})}"> <span th:text='${i+1}'>1</span></a>
                </li>
            </ul>
        </div>
    

    我遇到了这样一个错误:

    组织。百里香叶。例外情况。TemplateProcessingException:计算SpringEL表达式时出现异常:“#number.sequence(0,numerofpage.totalPages-1)”

    控制器中的代码:

    Page<BatchExecuteHistory> batchExecuteHistory = 
        batchExecuteHistoryService.getBatchExecuteHistory(id, pageable);
        model.addAttribute("jobDetailModel", jobDetailModel);
        model.addAttribute("batchExecuteHistory", batchExecuteHistory);
        model.addAttribute("nubmerOfPage", batchExecuteHistory.getContent());
        model.addAttribute("url", "/jobManagementDetail/{id}/{name}/{time}");
    

    我找到了根本问题:

    组织。springframework。表示spel公司。SpelEvaluationException:EL1008E:在“java”类型的对象上找不到属性或字段“totalPages”。util。集合$UnmodifiableRandomAccessList”-可能不是公共的?

    我在此链接中使用代码: Implement paging function with Spring Boot + Thymeleaf

    有人知道为什么吗?请帮忙

    1 回复  |  直到 7 年前
        1
  •  1
  •   M. Schreiber    7 年前

    在您提供的教程中,它指出您必须像这样使用控制器:

    @RequestMapping(value="/word/wordList", method=RequestMethod.GET)
    public String getWordList(Model model, Pageable pageable) {
        Page<Word> wordPage = wordService.getAllWord(pageable);
        PageWrapper<Word> page = new PageWrapper<Word>(wordPage, "/word/wordList");
        model.addAttribute("page", page); //HERE
        model.addAttribute("words", page.getContent());
    
        return "/word/wordList";
    }
    

    但您设置:

    model.addAttribute("nubmerOfPage", batchExecuteHistory.getContent());
    

    因此,将其设置为应该可以解决此问题:

    model.addAttribute("nubmerOfPage", batchExecuteHistory);