2016-09-14 125 views
0

我試圖用Spring Boot中的一些數據填充thymeleaf模板。 我想要做的就是這個Thymeleaf th:如果出現空錯誤

<tr th:if="${group.organization}"> 
    <td class="col_title"><b>Organization:</b></td> 
    <td class="organization-field-content" th:text="${group.organization}"></td> 
</tr> 

我已經試過這裏提出的兩種解決方案: Thymeleaf: show text if the attribute and property exists 和,以下thymeleaf的呈現順序,因爲group.organization爲空,整個內td小號不應顯示。

仍然存在一個問題,因爲Thymeleaf抱怨說

Servlet.service() for servlet [dispatcherServlet] in context with path [] 
threw exception [Request processing failed; nested exception is 
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating 
SpringEL expression: "group.organization" (group)] with root cause 
java.lang.NullPointerException: null 

我不明白爲什麼會這樣,因爲組對象存在,只是該組織爲null

+0

是'組織''布爾'屬性?如果不是這樣,則更有意義: ' <0123>組織: ' – bphilipnyc

回答

0

您可能要是真的確認你的組對象不是空的,只是在類似<span th:utext="${group}" />

0

這樣的跨度上傾倒它也許嘗試以下檢查組和組織在顯示div之前是否爲空。

<tr th:if="${group != null && group.organization != null}"> 
    <td class="col_title"><b>Organization:</b></td> 
    <td class="organization-field-content" th:text="${group.organization}"></td> 
</tr> 
0

感謝所有的答覆,原來問題在別的地方結束了。

所有提出的解決方案仍然給出了同樣的問題,這是因爲組Spring數據模型中的一種方法。 事實上,空指針異常來自getOrganization方法,該方法沒有正確序列化null值。

也許這可以幫助別人在將來

相關問題