【问题标题】:Thymeleaf - Spring . Accessing a object property inside another object百里香-春天。访问另一个对象内的对象属性
【发布时间】:2017-08-02 06:16:30
【问题描述】:

我有这门课

public class Guardian {

    public Guardian() {
        super();
    }

    private Long id;

    private String name;
..
}

public class AlarmNotification {


    private Long id;


    private Guardian guardian;
}

在我的 Thymeleaf 模板中

<td class="col_name" th:text="${alarmNotification.guardian.name}"></td>

但我得到了这个异常

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null

【问题讨论】:

  • AlarmNotification 中的监护人为空。您必须初始化监护人才能访问 name 属性。
  • 在控制器上设置
  • 可以添加控制器代码

标签: java spring spring-mvc thymeleaf spring-el


【解决方案1】:

监护人为空。添加三元运算符来处理这种情况:

<td class="col_name" th:text="${alarmNotification.guardian == null ? '' : alarmNotification.guardian.name}"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 2015-12-09
    • 1970-01-01
    • 2018-04-25
    相关资源
    最近更新 更多