spring-boot - 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>
解决方案
奇怪的是空检查不起作用。可能是命名有一些问题。无论如何,保存使用 null 对象的一种简短方法是使用save navigation
. 您将其与?
运算符一起使用:
<div th:if="${brackets?.brackets} != null">
推荐阅读
- javascript - How To disable zoom in/out webview but increase font-size on Pinch
- java - 如何从动态 JCheckBox 中获取价值?
- email - DMARC 失败仅适用于linkedin
- css - 如何使用 id 属性以角度导入样式表?
- regex - 如何从php数组中grep值
- java - json parsing with jackson
- window - Click on a specific place on a window, click on a specific button and control if the window changes
- html - How can I write HTML to my v-dialog's v-card component?
- powerbi - Read value from a different table for Y-axis constant line in Power BI
- python - 如何从 Rails 发送 json