2

春と一緒に thymeleaf を使用していますが、次の html セグメントを解析するときにエラーが発生します

<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 の設定で何か不足していますか?

4

2 に答える 2

2

このエラーは、initem.branch.branchNameオブジェクトbranchが null であるため、Thymeleaf がレンダリングできないことを意味します。このケースを処理するための三項演算子を追加します。

<tbody>
  ...
  <td th:text="${item.branch == null ? '' : item.branch.branchName}"/>
  ...
</tbody>
于 2016-07-14T02:38:08.653 に答える
1

@sanluckの回答に加えて、nullではないかどうかを確認する方が良いと思います。私の意見では、より高速で信頼性が高いからです。

<tbody>
  ...
  <td th:text="${item.branch != null ? item.branch.branchName : 'NOT FOUND'}"/>
  ...
</tbody>
于 2016-07-14T02:40:10.957 に答える