Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

FreeMarker is a Java-based template engine that is a great alternative to JSP. FreeMarker is ideal for situations where your action results can possibly be loaded from outside a Servlet container. For example, if you wished to support plugins in your application, you might wish to use FreeMarker so that the plugins could provide the entire action class and view in a single jar that is loaded from the classloader.

Table of Contents
minLevel2

Configure your action to use the "freemarker" result type

...

Code Block
Your name is: ${name}
Note
titleBe Careful

By default, FreeMarker will throw an error if it finds a variable that is not defined, or has a null value. See this FAQ for details.

Servlet / JSP Scoped Objects

...

In addition, you can specify a location (directory on your file system) through the templatePath or TemplatePath context variable (in the {{web.xml)}. If a variable is specified, the content of the directory it points to will be searched first.

...

If a property is defined on the template with the same name as a property on the action, FreeMarker will use the property defined on the template.

Note
titleBe Careful

By default, FreeMarker will throw an error if it finds a variable that is not defined, or has a null value. See this FAQ for details.

Variable Resolution

When using FreeMarker with the framework, variables are looked up in several different places, in this order:

...

Code Block
titlefreemarker.properties example
default_encoding=ISO-8859-1
template_update_delay=5
locale=no_NO

Tags

Using Struts tags

Tags distributed with Struts are automatically made available to FreeMarker templates. To use any tag add "@s." in front of the tag name. Like:

Code Block
HTML
HTML
titleUsing Struts tags on FreeMarker templates
<@s.if test="printName">
    <@s.property value="myBeanProperty" />
</@s.if>

Using JSP tags

To use JSP tags that are not part of Struts you have to:

...

If you want the framework to handle the formatting according to the Type Conversion you have specified, you shouldn't use the normal ${...} syntax. Instead, you should use the property tag. The difference is that the property tag is specifically designed to take an OGNL expression, evaluate it, and then convert it to a String using any Type Conversion rules you have specified. The normal ${...} syntax will use a FreeMarker expression language, evaluate it, and then convert it to a String using the built in formatting rules. (warning)

Note

The difference in how type conversion is handled under Freemarker is subtle but important to understand.

String and Non String Values on tags

In FreeMarker it is incorrect to quote non string values. If a value is quoted, then an string will be passed, instead of the expected object, causing an exception. For example, the "textarea" tag expects the attributes "rows" and "cols" of type Integer:

Code Block
XML
XML
titleDo not quote non string values in tag attributes!

<@s.textarea rows=5 cols=40 />

Extending

Sometimes you may with to extend the framework's FreeMarker support. For example, you might want to extend the Struts tags that come bundled with the framework.

...

Code Block
HTML
HTML
titleUse alternative syntax
[#ftl]
<html>
   <head>FreeMarker Example</head>
    
   <body>
       <h1>Alternative Syntax</h1>
       [@s.if test="printName"]
          [@s.property value="myBeanProperty" /]
       [/@s.if]
   </body>
</html>

String and Non String Values on tags

In FreeMarker it is incorrect to quote non string values. If a value is quoted, then an string will be passed, instead of the expected object, causing an exception. For example, the "textarea" tag expects the attributes "rows" and "cols" of type Integer:

Code Block
XMLXML
titleDo not quote non string values in tag attributes!

<@s.textarea rows=5 cols=40 />