Freemarker生成HTML错误

错误代码:

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>