`

Freemark is not specified异常的一点思考

阅读更多

  同事在调试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,在赋值给这个自定义变量。

 

3
3
分享到:
评论
1 楼 wuzhiyuzhi 2012-11-02  
谢谢,有帮助……

相关推荐

Global site tag (gtag.js) - Google Analytics