具有空值的Thymeleaf异常行为

时间:2018-05-03 17:10:13

标签: spring spring-mvc thymeleaf

我有一个Mongodb文档,如:

Document document = new Document()
document.append("_id", 1);
document.append("isTrue", true); //this is optional

我是如何在Thymeleaf中打印的。

<td th:text="${document.isTrue}"></td>

因此,由于isTrue是可选的,有时isTrue有时不存在。当document.isTrue存在时,它可以正常工作,但是当不存在时,Thymeleaf会给出错误

Exception evaluating SpringEL expression: "document.isTrue"

虽然我期望它应该打印null,因为当没有给定值的键匹配时,mongodb文档返回null。

1 个答案:

答案 0 :(得分:0)

我认为在这种情况下你应该使用:

<td th:text="${document['isTrue']}" />

在访问地图时,.运算符看起来比['key']更严格。或者,您也可以这样做:

<td th:text="${document.get('isTrue')}" />