首页 > 解决方案 > Thymeleaf 和 th:如果在 null 时不跳过文本

问题描述

我知道如果 Object 为空,您应该能够在百里香叶中使用 th:if 来“跳过”文本打印。

但是我的页面在第 23 行抛出 Null 错误。(第二个 div)

        <div th:if="${Brackets}">
            <th:block th:each="brackets : ${Brackets}">
                <div th:if="${brackets.brackets}">
                    <tr th:each="bracket : ${brackets.brackets}">
                        <td th:text="${bracket.lower}"></td>
                        <td th:text="${bracket.higher}"></td>
                        <td th:text="${bracket.prc}"></td>
                    </tr>
                </div>
            </th:block>
        </div>

我在这里想念什么?

用“?”解决 非常感谢帕特里克!

<div th:if="${Brackets} != null">
            <th:block th:each="brackets : ${Brackets}">
                <div th:if="${brackets?.brackets} != null">
                    <tr th:each="bracket : ${brackets?.brackets}">
                        <td th:text="${bracket.lower}"></td>
                        <td th:text="${bracket.higher}"></td>
                        <td th:text="${bracket.prc}"></td>
                    </tr>
                </div>
            </th:block>
        </div>

标签: spring-bootif-statementthymeleaf

解决方案


奇怪的是空检查不起作用。可能是命名有一些问题。无论如何,保存使用 null 对象的一种简短方法是使用save navigation. 您将其与?运算符一起使用:

<div th:if="${brackets?.brackets} != null">

推荐阅读