首页 > 解决方案 > 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>

`

标签: springthymeleaf

解决方案


您也可以使用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>

检查此参考


推荐阅读