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
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