【发布时间】:2017-04-28 23:31:53
【问题描述】:
我正在使用与 Spring-Boot 一起打包的 Thymeleaf。这是主要模板:
<div class="container">
<table th:replace="fragments/resultTable" th:if="${results}">
<tr>
<th>Talent</th>
<th>Score</th>
</tr>
<tr>
<td>Confidence</td>
<td>1.0</td>
</tr>
</table>
</div>
它使用了这个片段:
<table th:fragment="resultTable">
<tr>
<th>Talent</th>
<th>Score</th>
</tr>
<tr th:each="talent : ${talents}">
<td th:text="${talent}">Talent</td>
<td th:text="${results.getScore(talent)}">1.0</td>
</tr>
</table>
片段仅在有结果对象时才有效。这对我来说很有意义。因此,基于documentation 的语法,我将th:if 语句添加到主模板文件中。但是,当我在没有对象的情况下访问模板时仍然出现此错误
Attempted to call method getScore(com.model.Talent) on null context object
th:if 语句不应该阻止该代码被访问吗?
填充结果对象后,模板仍然可以正常工作,但是如何在没有表格的情况下呈现 null 情况?
【问题讨论】:
-
如何在片段本身内添加一个空检查
-
你的意思是如果 condition1 != null 和 condition2 = 什么?见:stackoverflow.com/questions/31524073/…
标签: spring-mvc spring-boot thymeleaf