2016-07-14 1 views
1

널 (null)에서 찾을 수없는 다음과 같은 HTML 세그먼트를 구문 분석 할 때 오류가있다org.springframework.expression.spel.SpelEvaluationException - 속성 또는 필드에 나가 봄과 함께 thymeleaf 사용하고

<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도 하나의 엔티티이고 하나의 속성 branchName을 포함합니다. HTML을 렌더링 할 때 그러나, 오류

2016-07-14 10:07:31.114 ERROR 8088 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine    : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "systemusers/list": Exception evaluating SpringEL expression: "item.branch.branchName" (systemusers/list:38) 
2016-07-14 10:07:31.116 ERROR 8088 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]  : Servlet.service() for servlet [dispatcherServlet] in context with path [/crpms] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "item.branch.branchName" (systemusers/list:38)] with root cause 
org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'branchName' cannot be found on null 

어떤 문제가있다? Thymeleaf의 구성에서 뭔가를 놓치고 있습니까?

+1

그림이 아닌 코드로 코드를 붙여 넣으십시오. – sanluck

+0

@sanluck 알았어, 고마워. 그것은 stackoverflow에서 질문을 내 첫 시간이야. – lupper

답변

1

이 오류는 item.branch.branchName 개체 branch이 null이므로 Thymeleaf에서 렌더링 할 수 없음을 의미합니다. 이 사건을 처리하는 삼항 연산자를 추가

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

그것은 작동합니다! 많은 감사합니다! – lupper

1

을 또한 대답을 @sanluck하기 위해, 나는 그것이 null가 아닌 경우가 더 빠르고 신뢰할 수있는 내 의견대로 확인하는 것이 좋습니다 생각 :

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

또한 귀하의 조언에 감사드립니다! – lupper

관련 문제