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. (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. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??

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

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

你可能感兴趣的:(传智健康项目,DeBug,java,spring,freemarker)