将 th:each 与 Themeleaf 一起使用并在空列表上应用分区抛出错误

Using th:each with Themeleaf and applying partitioning on empty list throw error

遍历酒店列表。每当列表为空时,它都会给我错误

     <div class="row list" th:if="${#lists.size(hotels) > 0}" th:each="hotelChunk : ${T(com.google.common.collect.Lists).partition(hotels, ${#lists.size(hotels)})}">
                <!-- single result item -->
                <div th:each="hotel : ${hotelChunk}"  class="col-md-4 single-result" >

错误是:

HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "T(com.google.common.collect.Lists).partition(hotels, ${#lists.size(hotels)})" (search:44)

这个问题有解决办法吗?

您必须将 th:each 更改为:

th:each="hotelChunk : ${T(com.google.common.collect.Lists).partition(hotels, #lists.size(hotels))}"

无需在当前 Thymeleaf 表达式中再次使用 ${..}