org.springframework.expression.spel.SpelEvaluationException: EL1007E: Свойство или поле 'items' нельзя найти в null - PullRequest
0 голосов
/ 01 мая 2020

Как решить эту ошибку? Я использую тимили вместе с пружиной, и при синтаксическом анализе следующего сегмента 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 голосов
/ 01 мая 2020

Вы также можете использовать 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>

Проверьте это ссылка

0 голосов
/ 01 мая 2020

Теперь появилась такая ошибка.

     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 голосов
/ 01 мая 2020

В своем комментарии вы сказали, что если вы добавили что-то в корзину, она работает, это означает, что shoppingCart существует в области действия сеанса, но в ShoppingCart не было никаких элементов.

Все вы нужно сначала проверить, существуют ли элементы. (Если его не существует, вам не нужно его показывать!)

<div th:if="${!session.shoppingCart.items}">
    your code
</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...