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.

Configure your action to use the "freemarker" result type

Struts provides a result type "freemarker" which renders a FreeMarker template. This result type is defined in struts-default.xml. To create pages using FreeMarker, set the result type of the actions to "freemarker".

Using properties

FreeMarker uses the ${...} notation to access properties. They are called interpolations. Properties on the actions (getter methods) will automatically be available on the FreeMarker templates. If an action has a "getName()" method, then its value can be inserted on the template like:

Be Careful

Icon

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.

Request Parameter

Context parameter

Assuming there's a parameter with the name myContextParam in framework context.

or

Template Loading

The framework looks for FreeMarker templates in two locations (in this order):

Web application

Class path

This ordering makes it ideal for providing templates inside a fully-built jar, but allowing for overrides of those templates to be defined in your web application. In fact, this is how you can override the default UI tags and Form Tags included with the framework.

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.

Icon

This variable is currently NOT relative to the root of your application.

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.

Variable Resolution

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

Built-in variables

Value stack

Action context

Request scope

Session scope

Application scope

Note that the action context is looked up after the value stack. This means that you can reference the variable without the typical preceding has marker (#) like you would have to when using the JSP s:property tag. This is a nice convenience, though be careful because there is a small chance it could trip you up.

The value stack itself, useful for calls like ${stack.findString('ognl expr')}

action

The action most recently executed

response

The HttpServletResponse

res

Same as response

request

The HttpServletRequest

req

Same as request

session

The HttpSession

application

The ServletContext

base

The request's context path

FreeMarker configuration

To configure the FreeMarker engine, just add a file freemarker.properties to the classpath. The supported properties are those that the FreeMarker Configuration object expects, see FreeMarker's documentation for more details.

freemarker.properties example

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:

Using Struts tags on FreeMarker templates

Using JSP tags

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

1. Add JspSupportSerlvet to web.xml

Adding JspSupportSerlvet to web.xml

2. Declare the tld on web.xml or use FreeMarker's "assign" directive. When using the "assign" directive, provide the absolute path to the tld file.

Using JSP tags on FreeMarker templates

Tips and Tricks

There are some advanced features that may be useful when building Struts applications with FreeMarker.

Type Conversion and Locales

FreeMarker has built in support for formatting dates and numbers. The formatting rules are based on the locale associated with the action request, which is by default set in struts.properties but can be over-ridden using the I18n Interceptor. This is normally perfect for your needs, but it is important to remember that these formatting rules are handled by FreeMarker and not by the framework's Type Conversion support.

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.

Icon

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:

Do not quote non string values in tag attributes!

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.

To extend the Freemarker support, develop a class that extends org.apache.struts2.views.freemarker.FreemarkerManager, overriding methods as needed, and plugin the class through the struts.properties:

FreeMarker alternative syntax

FreeMarker by default uses the "<#directive />" syntax. FreeMarker supports an alternative syntax, where [ and ] are used instead of < and >. To enable the alternative syntax, add [#ftl] at the beginning of the template. The alternative syntax makes it easier to differentiate between FreeMarker directives, and JSP or HTML tags.

Use alternative syntax

There are a number of IDE plugins available for FreeMarker. But, if your IDE is not on the list, then the using alternative syntax will avoid conflicts between FreeMarker and the HTML syntax highlighting provided by your IDE.