使用Spring Boot和Thymeleaf的分页时出错

时间:2018-04-10 08:46:44

标签: spring-boot pagination thymeleaf

我在下面有一些代码来分配从控制器发送的数据:

    <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

任何人都知道为什么?请帮忙

1 个答案:

答案 0 :(得分:1)

在您提供的教程中,它表示您必须像这样使用Controller:

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