【问题标题】:Thymeleaf abnormal behavior with null value具有空值的 Thymeleaf 异常行为
【发布时间】:2018-05-03 17:10:13
【问题描述】:

我有一个 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。

【问题讨论】:

    标签: spring spring-mvc thymeleaf


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-05
      • 2014-12-17
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多