spring - org.springframework.expression.spel.SpelEvaluationException:EL1007E:在 null 上找不到属性或字段“项目”
问题描述
如何解决这个错误?我将 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>
`
解决方案
您也可以使用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>
检查此参考
推荐阅读
- azure-devops - Azure devops .netcore 失败,但在 Visual Studio 中构建良好
- spring-saml - 创建名为 'org.springframework.security.saml.SAMLBootstrap#0' 的 bean 时出错
- excel - 计算列范围内两次之间的差异
- r - ggplot - 添加垂直线
- python - 如何选择列表中的特定元素并使用它们(Python)
- sql-server - 将数据解析为单独的列
- html - div 不自动换行
- ruby-on-rails - 如何通过循环对象数组来创建哈希数组
- javascript - 我在 Rails 上使用什么 JS 运行时?
- python - 如何对文本文件中的数字进行分类?