Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

FreeMarker: ??: Check for existence (null) of variable

Using ?? operator, we can check whether Variable exist or not.

 

Syntax

unsafe_expr?? or (unsafe_expr)??

 

?? return true if the variable exists and non-null.

 

Example1

Welcome ${name}!

Welcome Visitor
#if>

If the variable ‘name’ present and not null, then the text in if directive gets executed, else the snippet in else directive gets executed.

Example 2




${hobby}
#items>
#list>

No Hobbies entered for user
#if>

Find the below working application.

Step 1: Define nullCheck.ftl file under src/main/resources/templates folder.

nullCheck.ftl

Welcome ${name}!

Welcome Visitor
#if>





$
{hobby}
#items>
#list>

No Hobbies entered for user
#if>

Step 2: Define ‘FreeMarkerUtil’ class that take model class and template file as input and merge them.

FreeMarkerUtil.java
package com.sample.app.util;

import java.io.StringWriter;
import java.util.Locale;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class FreeMarkerUtil {

private static final Configuration FREE_MARKER_CONFIGURATION = new Configuration(Configuration.VERSION_2_3_30);

static {
FREE_MARKER_CONFIGURATION.setClassForTemplateLoading(FreeMarkerUtil.class, "/templates/");
FREE_MARKER_CONFIGURATION.setDefaultEncoding("UTF-8");
FREE_MARKER_CONFIGURATION.setLocale(Locale.US);
FREE_MARKER_CONFIGURATION.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
FREE_MARKER_CONFIGURATION.setFallbackOnNullLoopVariable(false);
}

public static StringWriter mergeModelAndTemplate(Object modelObject, String ftlFile) throws Exception {
StringWriter stringWriter = new StringWriter();

Template template = FREE_MARKER_CONFIGURATION.getTemplate(ftlFile);

template.process(modelObject, stringWriter);

return stringWriter;
}

}

Step 3: Define ‘NullCheckPopulator’.

NullCheckPopulator.java

package com.sample.app;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import com.sample.app.util.FreeMarkerUtil;

public class NullCheckPopulator {
public static void main(String args[]) throws Exception {

Map modelObject = new HashMap();

StringWriter stringWriter = FreeMarkerUtil.mergeModelAndTemplate(modelObject, "nullCheck.ftl");
System.out.println(stringWriter.toString().trim());

}
}

Output

Welcome Visitor


No Hobbies entered for user

?? vs non-top level variable

user.name??

This will handle if ‘name’ is missing inside the user (and returns "false" if so), but will not handle if user is missing. That is, the user variable itself must exist, otherwise, the template processing will die with an error.

(user.name)??

This will handle if user.name is missing. That is, if ‘user’ is missing, or ‘user’ exists but it does not contain ‘name’, the result will be "false", and no error will occur.



Previous                                                    Next                                                    Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

FreeMarker: ??: Check for existence (null) of variable

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×