freemarker判斷對象是否爲空

<#-- 取值 -->對象

  顯示某對象使用${field}或者${class.field}class

<#-- 若field爲null,或者可能爲null,則須要判斷對象是否爲空 -->字符

  但若是field爲null,freemarker就會報錯。若是須要判斷對象是否爲空freemarker

<#if field??>錯誤

  <#-- field存在... -->

  <#else>

  <#-- field不存在... -->

</#if>

也能夠經過設置默認值${field!''}來避免對象爲空的錯誤。若是field爲空,就以默認值(「!」後的字符)顯示。

對象class,field爲class的屬性的狀況,class,field都有可能爲空,那麼能夠寫成${(class.field)!''},表示user或者name爲null,都顯示爲空。判斷爲空

<#if class.field??>

  <#-- 都不爲空,進來 -->

</#if>

相關文章
相關標籤/搜索