org.springframework.expression.spel.SpelEvaluationException - 在null上找不到属性或字段

时间:2016-07-14 02:28:35

标签: spring thymeleaf spelevaluationexception

我使用百里香与春天一起使用,解析下面的html片段时出现错误

select 
    case 
       when location = 'Sydney' 
          then Days/7 
       else Days/7.5 
    end as Hours

实体<tbody> <tr th:each="item:${systemUsers}"> <td th:text="${item.username}"/> <td th:text="${item.fullName}"/> <td th:text="${item.mobile}"/> <td th:text="${item.enabled}"/> <td th:text="${item.manGrade}"/> <td th:text="${item.branch.branchName}"/> <td> <a th:href="@{/users/detail/{id}(id=${item.id})}" class="btn btn-info">Details</a> </td> <td> <a th:href="@{/users/edit/{id}(id=${item.id})}" class="btn btn-danger">Edit</a> </td> </tr> </tbody> 包含一个属性systemuser,它也是一个实体,包含一个属性branch。但是在渲染html时,出现了错误

branchName

有什么问题?我在Thymeleaf的配置中遗漏了什么吗?

2 个答案:

答案 0 :(得分:2)

此错误表示在item.branch.branchName对象branch中为空,因此Thymeleaf无法呈现它。添加三元运算符来处理这种情况:

<tbody>
  ...
  <td th:text="${item.branch == null ? '' : item.branch.branchName}"/>
  ...
</tbody>

答案 1 :(得分:1)

除了@sanluck的答案之外,我认为检查它是否为空是更好的,因为我认为它更快更可靠:

<tbody>
  ...
  <td th:text="${item.branch != null ? item.branch.branchName : 'NOT FOUND'}"/>
  ...
</tbody>
相关问题