FreeMarker is a free Java-based template engine, originally focusing on dynamic web page generation with MVC software architecture. However, it is a general purpose template engine, with no dependency on servlets or HTTP or HTML, and is thus often used for generating source code, configuration files or e-mails. by wikiPedia

Maven dependencies

Since with maven-based project, add these requirement to pom.xml


FreeMarker with Spring MVC

import freemarker.template.*;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

private Configuration configuration;
configuration = new Configuration(new Version(2, 3, 20));
configuration.setClassForTemplateLoading(FreeMarkerTemplateUtils.class, "/templates");
try {
  Template template = configuration.getTemplate("user_email.ftl");

  String body = FreeMarkerTemplateUtils.processTemplateIntoString(template, userTemplateVariable);
} catch (IOException | TemplateException e) {
  log.error("userTemplate error", e);

Write to file

Writer out = new FileWriter(dir + "/freemarker.html");
template.process(root, out);


Starting from freemarker 2.3.7, you can use this syntax :


or, if you want display a default text when the attribute is null :

${(object.attribute)!"default text"}