2016-07-14 31 views
1

ヌルで見つけることができず、次の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が含まれているエンティティ1つのプロパティbranchも存在し、1つのプロパティ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にあるため、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

関連する問題