debug|freemarker的InvalidReferenceException处理

用freemarker的时候,报了个异常,如下:

FreeMarker template error:
The following has evaluated to null or missing:
==> setmeal.remark  [in template "mobile_setmeal_detail.ftl" at line 34, column 37]
----
Tip: It's the step after the last dot that caused this error, not those before it.
----
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${setmeal.remark}  [in template "mobile_setmeal_detail.ftl" at line 34, column 35]
----
Java stack trace (for programmers):
----
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
    at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:131)
    at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:355)

---

密密麻麻的,真让人头疼,先检查下环境配的对不对好了,xml配置、模板地址啥的,检查后发现没问题,那进入到第二步,检查下具体异常是啥;

首先找下抛出的异常是啥,InvalidReferenceException,好的,百度一下这个异常咋回事,一搜,说是模板里面的放的值不能为空,好家伙,不能为空啊,那读读报的错是不是这个意思呢?看到异常里面写到:The following has evaluated to null or missing,说是下面有个字段为空了,所以不行,好家伙,看来真的是这个原因,下面还有俩tip,读一读:

Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??

 如果出错的表达式(就是我设置的setmeal.remark),引用了个空的值,那么可以这么做.....balabalblabla(不翻译了,比较懒)

 读了读下面的报错,基本上明白错误的地方了:就是我用于生成模板的数据,有些是空字段,而这个我并没有设置,因此模板引擎报错了。知道这个错误后事情就简单了,要么设置空字段默认值,要么传入的值不能为空即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值