如何解决这个错误?我将thymeleaf和spring一起使用,在解析下面的html片段时出现错误。
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'items' cannot be found on null当我向购物车添加一些东西时,它起作用了。问题是当它是空的时候。
`
<tr th:each="item : ${session.shoppingCart.items}">
<td th:text="${item.book.id}"></td>
<td th:text="${item.book.title}"></td>
<td><span th:text="${item.book.price}"></span>cash</td>
<td>
<form action="#" th:action="@{/cart/update}" method="post">
<input type="hidden" th:value="${item.book.id}" name="id"/>
<input type="number" min="1" th:value="${item.quantity}" name="qty"/>
<button type="submit">UPDATE</button>
</form>
</td>
<td><span th:text="${item.subTotal}"></span>cash</td>
<td>
<form action="#" th:action="@{/cart/remove}" method="post">
<input type="hidden" th:value="${item.book.id}" name="id"/>
<button type="submit">remove</button>
</form>
</td>
</tr>`
发布于 2020-05-01 22:42:48
在您的评论中,您说如果您向购物车中添加了一些东西它就可以工作,这意味着shoppingCart存在于session作用域中,但是shoppingCart中没有任何项。
所有你需要做的就是首先检查项目是否存在。(如果它不存在,您不必显示它!)
<div th:if="${!session.shoppingCart.items}">
your code
</div>发布于 2020-05-01 23:01:56
您也可以使用th:unless来实现这一点,并将您的代码放在div下,并具有如下属性:
<div class="itemslist" th:unless="${#lists.isEmpty(session.shoppingCart.items)}">
<tr th:each="item : ${session.shoppingCart.items}">
<td th:text="${item.book.id}"></td>
<td th:text="${item.book.title}"></td>
<td><span th:text="${item.book.price}"></span>cash</td>
<td>
<form action="#" th:action="@{/cart/update}" method="post">
<input type="hidden" th:value="${item.book.id}" name="id"/>
<input type="number" min="1" th:value="${item.quantity}" name="qty"/>
<button type="submit">UPDATE</button>
</form>
</td>
<td><span th:text="${item.subTotal}"></span>cash</td>
<td>
<form action="#" th:action="@{/cart/remove}" method="post">
<input type="hidden" th:value="${item.book.id}" name="id"/>
<button type="submit">remove</button>
</form>
</td>
</tr>
</div>检查此reference
发布于 2020-05-01 23:02:34
现在出现了这样的错误。
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "!session.shoppingCart.items"
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'items' cannot be found on null
<div th:if="${!session.shoppingCart.items}">
<tr th:each="item : ${session.shoppingCart.items}">
<td th:text="${item.book.id}"></td>
<td th:text="${item.book.title}"></td>
<td><span th:text="${item.book.price}"></span>cash</td>
<td>
<form action="#" th:action="@{/cart/update}" method="post">
<input type="hidden" th:value="${item.book.id}" name="id"/>
<input type="number" min="1" th:value="${item.quantity}" name="qty"/>
<button type="submit">UPDATE</button>
</form>
</td>
<td><span th:text="${item.subTotal}"></span>cash</td>
<td>
<form action="#" th:action="@{/cart/remove}" method="post">
<input type="hidden" th:value="${item.book.id}" name="id"/>
<button type="submit">remove</button>
</form>
</td>
</tr>
</div>https://stackoverflow.com/questions/61543407
复制相似问题