== : Test for Equality
!= : Test for Inequality
< : Less Than Operator
<= : Less than or equal to
> : Greater than operator
>= : Greater than or equal to operator
Note
Freemarker can interpret >, >= are end of the tags. To prevent this use gt instead of > and gte instead of >=. You can even use lt instead of <, lte instead of <=,
Find the below working application.
Step 1: Create ‘conditionalOperators.ftl’ file under src/main/resources/templates folder.
conditionalOperators.ftl
<#assign a = 10, b = 11>
a : ${a}
b : ${b}
is a equals to a : ${(a==a)?string}
is a equals to b : ${(a==b)?string}
is a not equals to a : ${(a!=a)?string}
is a not equals to b : ${(a!=b)?string}
is a less than a : ${(a<a)?string}
is a less than b : ${(a<b)?string}
is a less than or equal to a : ${(a<=a)?string}
is a less than or equal b : ${(a<=b)?string}
is a greater than a : ${(a>a)?string}
is a greater than b : ${(a>b)?string}
is a greater than or equal to a : ${(a>=a)?string}
is a greater than or equal to b : ${(a>=b)?string}
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 ‘ConditionalOperatorsPopulator.java’.
ConditionalOperatorsPopulator.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 ConditionalOperatorsPopulator {
public static void main(String args[]) throws Exception {
Map<String, Object> modelObject = new HashMap<String, Object>();
StringWriter stringWriter = FreeMarkerUtil.mergeModelAndTemplate(modelObject, "conditionalOperators.ftl");
System.out.println(stringWriter.toString().trim());
}
}
Output
a : 10
b : 11
is a equals to a : true
is a equals to b : false
is a not equals to a : false
is a not equals to b : true
is a less than a : false
is a less than b : true
is a less than or equal to a : true
is a less than or equal b : true
is a greater than a : false
is a greater than b : false
is a greater than or equal to a : true
is a greater than or equal to b : false
No comments:
Post a Comment