How to Solve FreeMarker template error

In the process of using freemarker, the following errors are often seen:

11 Dec 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)??

 

The template code is as follows:

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="UTF-8">  
<title>freemarker demo</title>  
</head>  
<body>  
${username} <br />  
${age}<br />  
${sex}  
</body>  
</html>

 Root cause: sex is not set, so an error is reported

Solution:

Add an exclamation mark after undeclared variables

${sex!}

 

 You can also set the default value, add the default value after the exclamation mark

${sex!'abc'}

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *