我在 Freemarker 中创建了函数:
<#function formatDate anyDate> <#assign dateFormat = read_from_configuration() /> <#if anyDate??> <#return anyDate?date(dateFormat) /> <#else > <#return '' /> </#if> </#function>
我这样调用它:${formatDate(object.someDate)}
。
一切正常,直到 someDate
为空。在那种情况下,我得到异常:
Error executing macro: formatDate required parameter: anyDate is not specified.
我该怎么做?如果参数值为 null,我希望该函数能够工作。
最佳答案
这是我所做的,这似乎在大多数情况下都有效:
默认值应该是一个空字符串,空值检查应该是?has_content。
<#function someFunction optionalParam="" >
<#if (optionalParam?has_content)>
<#-- NOT NULL -->
<#else>
<#-- NULL -->
</#if>
</#function>
关于java - 参数可以为空的 Freemarker 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4645784/