FreeMarker has nothing to do with web containers, that is, it does not know about Servlets or HTTP when running on the web. It can not only be used as an implementation technology for the presentation layer, but can also be used to generate XML, JSP or Java, etc.
To display an object in freemarker, use ${name}.
But if name is null, freemarker will report an error. If you need to determine whether the object is empty:
<#if name??> …… </#if>
Of course, you can also avoid the error of the object being empty by setting the default value ${name!''}. If name is empty, the default value (characters after "!") will be displayed.
If the object user and name are attributes of user, both user and name may be empty, then it can be written as ${(user.name)!''}, which means that user or name is null, both will be displayed as empty.
Judged to be empty
<#if (user.name)??> …… </#if>
Method 2:
For null or miss value, freemarker will report an error
!: default value operator, the syntax structure is: unsafe_expr!default_expr, such as ${mouse!"No mouse."} When mouse does not exist, return default value;
(product.color)!"red" This method can handle the situation where product or color is a miss value;
And product.color!"red" will only handle the case where color is miss value
??: Missing value test operator, tests whether it is missing value
unsafe_expr??: product.color?? will only test if color is null
(unsafe_expr)??: (product.color)?? will test whether product and color are null
?exists:Usage of old version
For example:
<#if mouse??> Mouse found <#else> No mouse found </#if> Creating mouse... <#assign mouse = "Jerry"> <#if mouse??> Mouse found <#else> No mouse found </#if>
The above uses two methods to introduce the method of freemarker to determine whether an object is empty. I hope you like it.