Search code examples
spring-bootpaginationthymeleaf

Error when using Pagination with Spring Boot and Thymeleaf


I have some codes below to paginate data sent from controller:

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

I got an error like this:

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#numbers.sequence(0, nubmerOfPage.totalPages-1)"

Code in controller:

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}");

I find out the root problem:

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'totalPages' cannot be found on object of type 'java.util.Collections$UnmodifiableRandomAccessList' - maybe not public?

I use code in this link: Implement paging function with Spring Boot + Thymeleaf

Anyone know why? Please help


Solution

  • In your provided tutorial it says that you have to use the Controller like this:

    @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";
    }
    

    But you set:

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

    So setting it to should fix it:

    model.addAttribute("nubmerOfPage", batchExecuteHistory);