freemarkers skip assertNonNull InvalidReferenceException

本秂侑毒 提交于 2020-01-21 19:20:11

问题


I render a list of Objects with freemarker:

<ul>
    <#list publication as item>
        <li><b>${item.key}</b> : ${item.value}</li>
    </#list>
</ul>

but some of the item have a item.value null that raises the exception:

freemarker.core.InvalidReferenceException: Expression item.value is undefined on line 12, column 44 in mailTemplate.
    at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:125)
    at freemarker.core.Expression.getStringValue(Expression.java:118)

I would like that freemarker renders "null" or nothing and keeps sending the mail instead of blocking on that

I can always check the list first before sending it to freemarker, but if i can avoid this step it will be better


回答1:


You should check in the template if the value is missing, and then print something that makes sense for the recipients ("null" certainly doesn't make sense for them). Like, N/A if the item needs no value:

<li><b>${item.key}</b> : ${item.value!'N/A'}</li>

or skip the whole line if the value is missing because the record is still incomplete:

<#if item.value??>
  <li><b>${item.key}</b> : ${item.value}</li>
</#if>


来源:https://stackoverflow.com/questions/11577482/freemarkers-skip-assertnonnull-invalidreferenceexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!