FreeMarker Hello World

FreeMarker Hello World

It’s one more template engine in the line to generate text output. It’s java class library to use in java programming. People generally use this template to generate HTML page and its good fit in any MVC application if you want to avoid JSP and keep your client page neat and clean. Basic idea behind this template is to keep HTML designer separate from java programmer where both can work independently. Java programmer writes application logic and designer works on html page design and finally all static data could be replace using FreeMarker engine. Below is high level diagram:

FreeMarker Hello World

To understand how this template works please follow below steps to Print hello world.

  • Maven project structure:

FreeMarker Hello World

Important: You will have to include freemarker.x.x.x.jar.

  • pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javahonk</groupId>
	<artifactId>FreeMarkerHelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>FreeMarkerHelloWorld</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.22</version>
		</dependency>
	</dependencies>
</project>
  • FreeMarkerHelloWorld.ftl
FreeMarker Template Hello World Example: ${name}
  • FreeMarkerHelloWorld.java
package com.javahonk;

import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;

public class FreeMarkerHelloWorld {
	
	private static Configuration cfg = null;
	
	static{		
		// Freemarker below configuration object deprecated
		//Configuration cfg = new Configuration();
		//Please use this. To make it backward compatible. Please visit here for more info: 
		//http://freemarker.org/docs/api/freemarker/template/Configuration.html
		cfg = new Configuration(new Version("2.3.0"));
	}
	
	public static void main(String[] args) {

		try {
			
			// Load template
			Template template = cfg.getTemplate("resources/FreeMarkerHelloWorld.ftl");

			// Create data for template
			Map<String, Object> templateData = new HashMap<String, Object>();
			templateData.put("name", "Java Honk");

			// Write output on console example 1
			StringWriter out = new StringWriter();
			template.process(templateData, out);
			System.out.println( out.getBuffer().toString() );
			out.flush();
			
			// Write output on console example 2
			/*Writer out = new OutputStreamWriter(System.out);
			template.process(templateData, out);
			out.flush();*/

			// File output
			Writer file = new FileWriter(new File("C:\\JavaHonk\\BuildXMLTemplateXML.ftl"));
			template.process(templateData, file);
			file.flush();
			file.close();

		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
}
  • Once you setup your project right click FreeMarkerHelloWorld.java –> Run As –> Java Application you will see below out on console and file:

FreeMarker Hello World

  • For more information please visit here

download  Download Project:  FreeMarkerHelloWorld

Leave a Reply

Your email address will not be published. Required fields are marked *