How to Solve FreeMarker template error

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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)??
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)??
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>freemarker demo</title>
</head>
<body>
${username} <br />
${age}<br />
${sex}
</body>
</html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>freemarker demo</title> </head> <body> ${username} <br /> ${age}<br /> ${sex} </body> </html>
<!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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
${sex!}
${sex!}
${sex!}

 

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
${sex!'abc'}
${sex!'abc'}
${sex!'abc'}

Read More:

Leave a Reply

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