【问题标题】:Error when using Pagination with Spring Boot and Thymeleaf将分页与 Spring Boot 和 Thymeleaf 一起使用时出错
【发布时间】:2018-09-19 19:31:11
【问题描述】:

下面有一些代码可以对控制器发送的数据进行分页:

    <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>

我收到这样的错误:

org.thymeleaf.exceptions.TemplateProcessingException:评估 SpringEL 表达式的异常:“#numbers.sequence(0, nubmerOfPage.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}");

我找到了根本问题:

org.springframework.expression.spel.SpelEvaluationException: EL1008E: 在“java.util.Collections$UnmodifiableRandomAccessList”类型的对象上找不到属性或字段“totalPages” - 可能不公开?

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

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

【问题讨论】:

    标签: spring-boot pagination thymeleaf


    【解决方案1】:

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

    @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);
    

    【讨论】:

    • 我修复了这个,但它仍然没有帮助:(
    • 我发现页面有一个类似 getTotalPages 的方法 :( 我试过 numberOfPage.totalPages 或 numberOfPage.getTotalPages() 但它不起作用
    猜你喜欢
    • 2023-03-29
    • 2018-11-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2022-01-24
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多