Components/Tools
FreeMarker provides a mechanism to transform/translate data from a single datasource
into multiple and diverse formats. FreeMarker comes with an Ant task called
FreemarkerXmlTask that can convert data from a source to HTML,
RTF, plain text, XML, or any other format. The FreemarkerXmlTask
is built for bulk conversions, so it can recursively convert complete folders
of documents in a single execution. However, it does not have advanced features like
generating output in multiple formats simultaneously. For advanced generation,
FreeMarker provdes a separate tool called FMPP.
FMPP is also an Ant task, but is much more feature-rich:
- Full support for the FreeMarker Template Language.
- Support for several data sourcesincluding XML file, comma-separated and
tab-separated text files, Ant build files, Java properties files, and plain text files.
- Source files validation before processing.
- Ordering and checking dependencies between files before processing.
- Error reporting, etc.
US Cavalry:
US Cavalry converts Velocity templates into FreeMarker templates. This tool can read
any Velocity 1.2 version templates and convert them into the latest FreeMarker
templates. However, the tool cannot transform the Velocity Java code into
FreeMarker-specific code, you need to do that manually.
IDE Plug-in
There are several IDEs and editors that have plug-ins for working with FreeMarker
templates. These include the Eclipse IDE (versions 2.x & 3.x), jEdit, and SitePad
Pro. Features available in the Eclipse IDE include syntax highlighting, code completion,
and automatic error detection. Other popular editors like EMacs, KWrite, TextPad, and
Vim provide support for syntax highlighting of FreeMarker code through simple add-ons.
Installation/Configuration:
To use FreeMarker, simply include the freemarker.jar into a classpath
where the application can find it. In the case of Web applications, the best place
would be the WEB-INF/lib directory of the Web application. To use
FreeMarker with XPath support you will need to include Jaxen binaries.
Practical Applications
This article provides two examples of how FreeMarker can be used. The first involves
using FreeMarker as a code generator to generate DAO classes, the second
example shows using FreeMarker in a Web application. Both examples provide a
good idea on how FreeMarker can be used in everyday applications.
Here are the pre-requisites for running the examples:
- Apache Ant
- JDK 1.4 or above
- MySQL Database 5
- MySQL Connector J
- Apache Tomcat 5.5 (the WAR file has been generated for Tomcat, but changes can
be made to run it on any other server).
-
FreeMarker.jar (these examples use version 2.3.6, the latest stable download).
-
Jaxen-full.jar and Saxpath.jar (optionalthese are needed
only if you plan to use XPath)
Both examples use the Northwind database on MySQL. The schema for the database can be found in the
sample download in the references section. The demo applications are not optimized for performance,
they are merely to demonstrate FreeMarker's capabilities.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.