首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >org.springframework.expression.spel.SpelEvaluationException: EL1007E:在null上找不到属性或字段“items”

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

Stack Overflow用户
提问于 2020-05-01 21:28:07
回答 3查看 273关注 0票数 0

如何解决这个错误?我将thymeleaf和spring一起使用,在解析下面的html片段时出现错误。

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'items' cannot be found on null当我向购物车添加一些东西时,它起作用了。问题是当它是空的时候。

`

代码语言:javascript
复制
<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>

`

EN

回答 3

Stack Overflow用户

发布于 2020-05-01 22:42:48

在您的评论中,您说如果您向购物车中添加了一些东西它就可以工作,这意味着shoppingCart存在于session作用域中,但是shoppingCart中没有任何项。

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

代码语言:javascript
复制
<div th:if="${!session.shoppingCart.items}">
    your code
</div>
票数 0
EN

Stack Overflow用户

发布于 2020-05-01 23:01:56

您也可以使用th:unless来实现这一点,并将您的代码放在div下,并具有如下属性:

代码语言:javascript
复制
<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

票数 0
EN

Stack Overflow用户

发布于 2020-05-01 23:02:34

现在出现了这样的错误。

代码语言:javascript
复制
     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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61543407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档