Thymeleaf - 春天。访问另一个对象

时间:2017-03-12 02:50:38

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

我有这个课程

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

1 个答案:

答案 0 :(得分:0)

守护者无效。添加三元运算符来处理这种情况:

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