FreeMarker template error

在使用freemarker的過程當中常常會見到以下錯誤:html

Xml代碼   收藏代碼
  1. 11 十二月 2015 15:53:09,674 ERROR freemarker.runtime:98 - Error executing FreeMarker template  
  2. FreeMarker template error:  
  3. The following has evaluated to null or missing:  
  4. ==> sex  [in template "freemarker3.html" at line 10, column 3]  
  5.   
  6. 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)??  

模板代碼以下:express

Html代碼   收藏代碼
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title>freemarker demo</title>  
  6. </head>  
  7. <body>  
  8. ${username} <br />  
  9. ${age}<br />  
  10. ${sex}  
  11. </body>  
  12. </html>  

 根本緣由: sex沒有設置值,因此報錯lua

解決方法:spa

在未聲明的變量後面增長嘆號xml

Html代碼   收藏代碼
  1. ${sex!}  

 也能夠設置默認值,在歎號後面增長默認值htm

Html代碼   收藏代碼
  1. ${sex!'abc'}  
相關文章
相關標籤/搜索