问题:使用Thymeleaf,当数据中有空值时,会报以下错误
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "emp.dept.name" (template: "employee/list" - line 52, col 21)
原因:
获取的数据emp中的dept为null,被拿来获取属性name,因此报错
解决:
加上问号,表示当前值为空时就不继续取值,直接显示空白
原代码: