1. Introduction to FreeMarker
FreeMarker is a Java based template engine which can be used as stand-alone or servlet bases Java programs. You write a FreeMarker template in a plain text file with the place holders like ${id}. This file is saved with .ftl extension. Not only the simple text, we can also use the conditions, loops, switch statements, etc. in the templates that can be dynamically supplied by the Java program. The Java program passes the actual value for the place holders defined in the FreeMarker template files.
The output of the template is written to the Writer object which you have provide and then this object can be passed to HTTP, File, etc. for printing the result. Programmer can configure the location of the templates and where the files has to be created with the results. This tutorial explains on how to create a simple FreeMarker template implementation in the stand alone mode.
2. How to Install FreeMarker?
Installing FreeMarker template engine is as simple as adding a JSR files to your project. You can download the latest version of the lobraries from freemarker website. At this time of writing this article, the latest version available for download is 2.3.20. If you are using the Maven build for your project, the add the following entry in your POM.xml.
<!-- Attention: Be sure nothing pulls in an old dependency with groupId "freemarker" (without the "org."), because then you will end up with two freemarker.jar-s and unpredictable behavior! --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency>
3. FreeMarker Java Template Processing
This is the important part of supplying the template place holders to the actual templates.
package javabeat.net.freemarker; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class FreeMarkerHelloWorld { public static void main(String[] args) { // Configurae Freemarker Configuration cfg = new Configuration(); try { // Load the template Template template = cfg.getTemplate("src/template.ftl"); Map<String, Object> data = new HashMap<String, Object>(); data.put("message", "Hello World!"); // Language list List<String> language = new ArrayList<String>(); language.add("Java"); language.add("C++"); language.add("Ceylon"); language.add("Cobol"); data.put("languages", language); Writer out = new OutputStreamWriter(System.out); template.process(data, out); out.flush(); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } } }
4. Create FreeMarker Template
FreeMarker Hello World Example : ${message} <#list languages as language> ${language_index + 1}. ${language} </#list>
5. Run Sample Application
If you run the above program, you would get the following output.
</span> <pre>FreeMarker Hello World Example : Hello World! 1. Java 2. C++ 3. Ceylon 4. Cobol