Freemarker Configuration with ClassTemplateLoader Example

By Arvind Rai, October 18, 2015
On this page, we will provide Freemarker Configuration with ClassTemplateLoader example. ClassTemplateLoader is used to load templates from classpath. To fetch loader we will use getTemplate() method as usual. To resolve the Freemarker JAR dependency, we are using Gradle as follows.
compile 'org.freemarker:freemarker:2.3.23'
 

Project Structure in Eclipse

Find the project structure in eclipse.
Freemarker Configuration with ClassTemplateLoader Example

ClassTemplateLoader

freemarker.cache.ClassTemplateLoader loads Freemarker templates from classpath. It can load templates from anywhere where java can load classes from, for example JAR file. To instantiate ClassTemplateLoader, Freemarker provides following constructor.
ClassTemplateLoader(Class resourceLoaderClass, String basePackagePath)
 

resourceLoaderClass : we can simply achieve it by getClass() method.
basePackagePath : If our package is com.concretepage, then basePackagePath will be /com/concretepage

Sample Template used in Demo

In our resources directory which is configured as classpath, we have created a directory as ftl and inside it, we have put our FTL template. Find the sample FTL template being used in our demo.
template.ftl
PM Name : ${pmName}

Country : ${country}  

Create Configuration with ClassTemplateLoader

Find the singleton class for Freemarker Configuration. We are instantiating it, by using ClassTemplateLoader.
ConfigurationUtil.java
package com.concretepage.freemarker;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
public class ConfigurationUtil {
    private static Configuration configuration ;
    static {
        configuration = new Configuration(Configuration.VERSION_2_3_23);
        ClassTemplateLoader loader = new ClassTemplateLoader(
        		new ConfigurationUtil().getClass(), "/ftl");
        configuration.setTemplateLoader(loader);
        configuration.setDefaultEncoding("UTF-8");
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    }
    public static Configuration getConfiguration() {
       return configuration;
    }
} 

Main Class

Find the main class to run the demo.
ClassTemplateLoaderDemo.java
package com.concretepage.freemarker;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class ClassTemplateLoaderDemo {
	public static void main(String[] args) {
         try {
            Configuration cfg = ConfigurationUtil.getConfiguration();
            //Create Data Model 
            Map<String, Object> map = new HashMap<>();
            map.put("pmName", "NaMo");
            map.put("country", "India");
            Template template = cfg.getTemplate("template.ftl");
            //Console output for template
            Writer console = new OutputStreamWriter(System.out);
            template.process(map, console);
            console.flush();
            //File output
            Writer file = new FileWriter (new File("template-output.html"));
            template.process(map, file);
            file.flush();
            file.close();
         } catch (IOException e) {
            e.printStackTrace();
         } catch (TemplateException e) {
            e.printStackTrace();
         }
 	}
} 
Find the output.
PM Name : NaMo

Country : India  

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us