Apache FreeMarker is a free, open supply, Java primarily based template engine which let you create dynamic content material by combining the static template with dynamic knowledge. The template is written in their very own proprietary language known as FTL(FreeMarker Template language), which is like every scripting language which lets you insert variables, looping constructs and conditional logic. FreeMarker is commonly used with JSP in MVC primarily based Java software to generate dynamic content material nevertheless it’s not restricted to Servlet or JSP and you should use with core Java as effectively. In truth, this FreeMarker Hey World Instance is with out JSP. We generate a dynamic HTML file by utilizing Freemarker within the fundamental() technique itself. Apache FreeMarker is commonly used for producing supply code, configuration recordsdata or personalised e-mails.
Instruments used on this Instance
1. Java
2. Eclipse
3. FreeMarker
Mission Dependencies
It is a FreeMarker howdy world program and it solely wants freemarker JAR and its dependencies. In case you are utilizing Maven, you possibly can simply add following dependency in your pom.xml file, this is not going to solely obtain freemarker JAR but in addition all of the dependent JAR freemarker wants.
<dependencies>
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<model>2.3.9</model>
</dependency>
</dependencies>
Alternatively, in case you are not utilizing Maven, you possibly can add following JAR recordsdata manually in your Java program’s classpath. In case you are unsure set classpath in Eclipse, checkout right here.
.m2orgfreemarkerfreemarker2.3.9freemarker-2.3.9.jar
FreeMarker HelloWorld Instance
To be able to create howdy world in freemarker, you want two issues, first a freemarker template and second an information mannequin. You additionally want a Java class to mix template and knowledge mannequin, however your Java class also can present knowledge mannequin when it comes to objects. Alternatively, you should use XML file to supply knowledge.
freemarker.ftl
That is the freemarker template file. It should be in your Java program’s classpath to be picked up by this system which is utilizing freemarker.
<html>
<head>
<title>Welcome! to FreeMarker HelloWorld Instance</title>
</head>
<physique>
<h1>Welcome ${person}!</h1>
<p>Our newest course : ${latestCourse.title}
with worth is ${latestCourse.worth}</p>
</physique>
</html>
You may see that template is producing an HTML file with person and course coming as dynamic content material. This knowledge will come from knowledge mannequin, which is a HashMap in our howdy world tutorial, nevertheless it might be an XML file as effectively in actual world.
Course.java
It is a Java bean, therefore it should be public. This class is a part of our knowledge mannequin, FreeMarker will extract latestCourse.title and latestCourse.worth worth from this class, therefore this class ought to have a reputation and worth attribute and corresponding public getter strategies.
public class Course {
personal String title;
personal lengthy worth;
public Course(String title, lengthy worth) {
this.title = title;
this.worth = worth;
}
public String getName() {
return title;
}
public lengthy getPrice() {
return worth;
}
public void setName(String title) {
this.title = title;
}
public void setPrice(lengthy worth) {
this.worth = worth;
}
}
Make sure that this class is public in any other case your FreeMarker Hey World program is not going to run and provides following error:
Exception in thread “fundamental”
Expression latestCourse.title is undefined on line 7, column 28 in freemarker.ftl.
The problematic instruction:
———-
==> ${latestCourse.title} [on line 7, column 26 in freemarker.ftl]
———-
Java backtrace for programmers:
———-
freemarker.core.InvalidReferenceException: Expression latestCourse.title is undefined on line 7, column 28 in freemarker.ftl.
at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)
at freemarker.core.Expression.getStringValue(Expression.java:118)
at freemarker.core.Expression.getStringValue(Expression.java:93)
at freemarker.core.DollarVariable.settle for(DollarVariable.java:76)
at freemarker.core.Setting.go to(Setting.java:196)
at freemarker.core.MixedContent.settle for(MixedContent.java:92)
at freemarker.core.Setting.go to(Setting.java:196)
at freemarker.core.Setting.course of(Setting.java:176)
at freemarker.template.Template.course of(Template.java:232)
at FreeMarkerHelloWorld.fundamental(FreeMarkerHelloWorld.java:37)
to keep away from this error simply make it possible for your Course class is public.
FreeMarkerHelloWorld.java
That is the principle class which can drive this system. It’s chargeable for creating freeMarker configuration and mixing freemarker template with knowledge mannequin. On this tutorial, we now have offered knowledge in HashMap, you possibly can see its secret’s the variable title used within the template file and values are the textual content which ought to substitute these variables.
In actual world you possibly can populate this Map with an XML file or from database.
import java.io.OutputStreamWriter;
import java.io.Author;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class FreeMarkerHelloWorld {
public static void fundamental(String...args) throws Exception {
Configuration cfg = new Configuration();
Map < String, Object > mannequin = new HashMap < String, Object > ();
mannequin.put("person", "John");
mannequin.put("latestCourse",
new Course("FreeMarker Newbie Course",
100));
Template t = cfg.getTemplate("freemarker.ftl");
Author out = new OutputStreamWriter(System.out);
t.course of(mannequin, out);
System.out.println("Carried out..");
}
}
If you run this program, you’re going to get following output
<html>
<head>
<title>Welcome! to FreeMarker HelloWorld Instance</title>
</head>
<physique>
<h1>Welcome John!</h1>
<p>Our newest course : FreeMarker Newbie Course
with worth is 100</p>
</physique>
</html>Carried out..
In our instance, I’ve printed the output in console however you can even write output into an HTML file by utilizing a FileWriter object as defined right here.
That is all about freemarker howdy world instance in Java. As I advised you should use this library to create dynamic content material even on core Java undertaking nevertheless it largely used together with JSP in MVC initiatives. I’ve used this expertise in one of many essential Java undertaking in previous the place we show a number of static and dynamic knowledge utilizing Apache FreeMarker.