System Flow/Architecture
FreeMarker's strength is derived in separating the model layer from the
presentation layer. FreeMarker does this by having a data model to represent
the model layer and a template to represent the presentation layer. Marrying
the data model and the template produces the desired result. The result could
be Java classes, HTML, XML, plain text, RTF, or any output. This paradigm makes
it easier to separate the business logic from the presentation cleanly and gives
developers and designers the independence they crave. Normally, developers
can't start working on the user interface until the designers are done with the
look and feel. Then, developers are forced to wade through tons of HTML
tags, stylesheets, and DHTML code to insert the business logic to make the page
dynamic. Once the business logic is embedded, the desired look and feel is often
not achieved due to misaligned CSS tags or incomplete HTML elements. Designers then
have to make modifications again to regain the look and feel. This reworking process
costs time, money, and makes code maintenance a nightmare. Just imagine having to
make modifications to the page!
FreeMarker allows programmers and developers to work independently on the model
layer and the view layer without stepping on each other’s toes. A designer can
start working on the presentation layer with just the outline of a data model.
The designer creates the presentation layer in their tool of choice and references
elements in the data model using a simple scripting language. Meanwhile, developers
can independently work on the model layer and populate the data model
with the actual values that need to be inserted into the presentation layer.
The Data Model
The data model is a collection of data arranged in a tree format. At the very
beginning is the root element and from it sprouts the different branches of data.
Branches have logical groups of data that are specific to a view. They can be
independent or shared, depending on the requirements of the presentation layer.
The data in a branch can be anything from simple data types like integer, string, or
Boolean to complex data types like maps, lists, and Java beans. FreeMarker converts
the DataModel to objects of the type freemarker.template.TemplateModel.
It uses an ObjectWrapper (freemarker.template.ObjectWrapper) to wrap
the Java type in an appropriate TemplateModel. For instance, strings are wrapped as
SimpleScalar, a Java object of type Number maybe wrapped by
SimpleNumber and so on. Java Beans and other objects are wrapped using
freemarker.ext.beans.BeansWrapper. The wrapping of Java beans is done through
Java reflection.
Templates
A template is a file that contains static text interpolated with special FreeMarker
tags in them. The tags give the dynamic quality to the template and can be used to
insert data dynamically from the model. As discussed earlier, FreeMarker tags are
written in the FreeMarker Template Language (FTL). They are straightforward and can
be used by any designer with a little experience with scripting languages.
FreeMarker Template Language (FTL):
Interpolation is the process by which part of a string, which is marked with special
tags, is processed and substituted with processed output before the whole string is
printed to the target output stream. Here's the syntax for string interpolation:
${ variable_name }
Everything that needs interpolation must be within the ${ and }
tags. The variable_name should be an element from the DataModel.
FTL Directives enable users to process and format data just like you would in a
regular programming language. Some of the built-in directives provided by
FreeMarker are loop constructs, inclusion of external data, decision points
etc.
FTL Directives are similar to HTML tags, in that:
- They must be in angular brackets
<>, with the difference
being that the first character after the start angular bracket must be the #.
For instance, <#list>.
- Every tag has a start and end tag. The end tag should be the same as the starting tag.
- Every tag must be properly nested.
FTL Expressions are a combination of variables from the DataModel, arithmetic
operators, logical operators, interpolations, etc. FTL Expressions are used to
gather and process data from different elements before they are evaluated. This
allows for scripting language-like flexibility within the presentation layer.
After the template has been loaded, it is parsed, merged with the DataModel, and
then printed to the desired output stream. Once a template has been parsed, it is
cached and subsequent requests for the same template results in the same
instance being returned. This results in optimal use of memory and enhances execution
speed.
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.
|