Freemarker错误和一些常用配置

0

错误代码:

0          ERROR      [main]                       freemarker.runtime  --Error executing FreeMarker template 03 七月 2015 14:25:32,730 
FreeMarker template error:
The following has evaluated to null or missing:
==> json  [in template "template.ftl" at line 56, column 49]
 
Tip: If the failing expression is known to be legally null/missing, either specify a default value with 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 parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
 
The failing instruction (FTL stack trace):
----------
==> ${json[0]}  [in template "template.ftl" at line 56, column 47]
----------
 
Java stack trace (for programmers):
----------
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
    at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:98)
    at freemarker.core.DynamicKeyName._eval(DynamicKeyName.java:87)
    at freemarker.core.Expression.eval(Expression.java:111)
    at freemarker.core.Expression.evalAndCoerceToString(Expression.java:115)
    at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
    at freemarker.core.Environment.visit(Environment.java:265)
    at freemarker.core.MixedContent.accept(MixedContent.java:93)
    at freemarker.core.Environment.visit(Environment.java:265)
    at freemarker.core.Environment.process(Environment.java:243)
    at freemarker.template.Template.process(Template.java:277)
    at com.test.resume.FreemarkerUtils.build(FreemarkerUtils.java:64)
    at com.test.resume.ResumeExport.export(ResumeExport.java:44)
    at com.test.resume.ResumeExport.main(ResumeExport.java:16)
FreeMarker template error:
The following has evaluated to null or missing:
==> json  [in template "template.ftl" at line 56, column 49]
 
Tip: If the failing expression is known to be legally null/missing, either specify a default value with 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 parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
 
The failing instruction (FTL stack trace):
----------
==> ${json[0]}  [in template "template.ftl" at line 56, column 47]
----------
 
Java stack trace (for programmers):
----------
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
    at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:98)
    at freemarker.core.DynamicKeyName._eval(DynamicKeyName.java:87)
    at freemarker.core.Expression.eval(Expression.java:111)
    at freemarker.core.Expression.evalAndCoerceToString(Expression.java:115)
    at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
    at freemarker.core.Environment.visit(Environment.java:265)
    at freemarker.core.MixedContent.accept(MixedContent.java:93)
    at freemarker.core.Environment.visit(Environment.java:265)
    at freemarker.core.Environment.process(Environment.java:243)
    at freemarker.template.Template.process(Template.java:277)
    at com.test.resume.FreemarkerUtils.build(FreemarkerUtils.java:64)
    at com.test.resume.ResumeExport.export(ResumeExport.java:44)
    at com.test.resume.ResumeExport.main(ResumeExport.java:16)

这里是用Freemarker来解析的JSON字符串,但是JSON是没有问题的,而且也不为null,而且网站使用Freemarker作为view都没出现这问题,最后我看了一下配置,原来需要有这么一项配置:

cfg.setClassicCompatible(true);

这个就主要用来处理空值的。

另外Freemarker中设置就是标签的格式的方法setTagSyntax,就是配置标签是[#if]还是<#if>

Freemarker标签输出内容不会出现空白行,就是和JSP的trimDirectiveWhitespaces配置一样的效果,只需要把标签和输出内容不放在同一行即可。

template_exception_handler配置Freemarker发生异常不显示在前台页面,这样可以避免泄露一些重要信息。