Freemarker hello world

Freemarker is a templating engine. You can use freemarker to generate any textual data which is a combination of static tex t written in freemarker template (.ftl) file  and some dynamic content generated by java code. This dynamic java content can come from some xml file, database, or something generated by java code itself. The Freemarker template and data are merged to generate string data.

Freemarker can be used in User interfaces instead of JSPs. Apart from this freemarker can also be used in code generators, Web template generators, Xml generators, creating bulk email body with customized content to each user etc.

Below is a basic example of using freemarker with Java.

The program simply takes data from a xml file “freemarker.xml” and merges it with the freemarker template “freemarker.ftl”

freemarker.xml

<recipients>
    <person>
        <name>Bharat Sharma</name>
        <address>Mumbai, Maharashtra</address>
    </person>
    <person>
        <name>Vijaya Sharma</name>
        <address>Nagpur, Maharashtra</address>
    </person>
</recipients>

freemarker.ftl

[#ftl]
[#foreach recipient in doc.recipients.person]
To: ${recipient.name}
    ${recipient.address}

Dear ${recipient.name},

Thank you for showing interest in this Freemarker tutorial.

Kind Regards,
Bharat Sharma
---------------------------------------------------------------------------------------
[/#foreach]

Java Code:

package com.bharat.freemarker;

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

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

/**
 * This program take data from an xml and merges it with a freemarker template
 * to generate output.
 * 
 * @author Bharat Sharma
 */
public class FreemarkerTest
{
  public static void main(String[] args)
  {
    Map<String, Object> tree = new HashMap<String, Object>();
    File document = new File("freemarker.xml");

    // Read the XML file and process the template using FreeMarker
    try
    {
      tree.put("doc", freemarker.ext.dom.NodeModel.parse(document));
      Configuration cfg = new Configuration();
      Template t = cfg.getTemplate("freemarker.ftl");
      Writer writer = new FileWriter(new File("freemarker-output.txt"));
      t.process(tree, writer);
      System.out.println("Done..");
    }
    catch (Exception e)
    {
      System.out.println(e.getLocalizedMessage());
    }
  }
}

The output of above program looks like below.

To: Bharat Sharma
    Mumbai, Maharashtra

Dear Bharat Sharma,

Thank you for showing interest in this Freemarker tutorial.

Kind Regards,
Bharat Sharma
---------------------------------------------------------------------------------------
To: Vijaya Sharma
    Nagpur, Maharashtra

Dear Vijaya Sharma,

Thank you for showing interest in this Freemarker tutorial.

Kind Regards,
Bharat Sharma
---------------------------------------------------------------------------------------
Advertisements

, , , ,

  1. #1 by Luca on January 11, 2013 - 3:26 pm

    I exactly copied the sample in eclipse. I’m using freemarker 2.3.19.
    My System.out is correctly “Done..”
    But, my generated file is empty. Why?
    Thanks
    Luca

  2. #2 by Nabil on January 31, 2013 - 2:37 pm

    Hello,

    your example does not work, i have somme erreur :

    javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
    nested exception is:
    java.net.SocketException: Connection reset
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at com.exemple.mail.SendMail.main(SendMail.java:95)
    Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:293)
    at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:331)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:798)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:548)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:352)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
    … 4 more
    Done….

  3. #3 by Nabil on January 31, 2013 - 3:31 pm

    Hi Loca ,
    on eclipse, you’re also import and Existing Project into workspeace, you choose the downloaded unzipped normally get your project on eclipse.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: