在使用freemarker的過程中經常會見到如下錯誤:
- 11 十二月 2015 15:53:09,674 ERROR freemarker.runtime:98 - Error executing FreeMarker template
- FreeMarker template error:
- The following has evaluated to null or missing:
- ==> sex [in template "freemarker3.html" at line 10, column 3]
- 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)??
模板代碼如下:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>freemarker demo</title>
- </head>
- <body>
- ${username} <br />
- ${age}<br />
- ${sex}
- </body>
- </html>
根本原因: sex沒有設置值,所以報錯
解決方法:
在未聲明的變量后面增加嘆號
- ${sex!}
也可以設置默認值,在嘆號后面增加默認值
- ${sex!'abc'}