org.springframework.expression.spel.SpelEvaluationException:EL1007E:在null上找不到属性或字段“ items”

时间:2020-05-01 13:28:06

标签: spring 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>

`

3 个答案:

答案 0 :(得分:0)

在您的评论中,您说过,如果您在购物车中添加了东西,这意味着session范围中存在shoppingCart,但是shoppingCart中没有任何物品。

您需要做的就是首先检查项目是否存在。 (如果它不存在,则不必显示它!)

<div th:if="${!session.shoppingCart.items}">
    your code
</div>

答案 1 :(得分:0)

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

答案 2 :(得分:0)

现在出现了这样的错误。

     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>
相关问题