Freemark is not specified异常的一点思考

本文介绍了一种解决Freemarker宏执行时因itemList参数未指定而导致的错误的方法。通过增加判断语句来检查对象是否为空,避免了运行时出现的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  同事在调试Freemark遇到如下错误:

Error executing macro: freeBackTable required parameter: itemList is not specified.……

 

  由于此前我没有了解过Freemark。因此,google中检索了一下,很多帖子说是空值问题。同事认为不是。但是,随后我在阅读Freemark官方的FAQ中发现有如下说明。因此,我认为这是null值引起的。

 

17. What about null and the FreeMarker template language?  
  The FreeMarker template language doesn't know the Java language null at all. It doesn't have null keyword, and it can't test if something is null or not. When it technically faces with a null, it treats it exactly as a missing variable.

 

  也就是说当FreeMarker程序中遇到null引用,会认为这个值没有声明。于是运行项目发现模板的908行报同样错误。其中有形如如下语句<@itemList=freeBackTable.itemList/>。因此我怀疑Struts2的对象为null,从而导致宏语句

“<#macro freeBackTable project  reformingFeedBack itemList checkNotifyingFM index>”中声明的itemList引用了一个空值,按照官方的说明,相当于“a missing variable”也就是说“not specified”。

  因此,加上Freemark的判断,在对“itemList”赋值语句前增加如下判断:“<#if freeBackTable.itemList?exists && freeBackTable.itemList?has_content>”,结果如下所示

<#if freeBackTable.itemList?exists && freeBackTable.itemList?has_content>
         <@itemList=freeBackTable.itemList />
</#if>

 

  再次运行程序,问题解决。在使用Freemark自定义的变量前,如果变量引用了一个第三方传递过来的对象,应该先判断该对像是否为null,在赋值给这个自定义变量。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值