This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • Using Freemarker Templates
Skip to end of metadata
Go to start of metadata

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".

<include file="struts-default.xml"/>
<action name="test" class="package.Test">
  <result type="freemarker">/WEB-INF/views/testView.ftl</result>

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:

Your name is: ${name}

Be 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

The following are ways to obtained Application scope attributes, Session scope attributes, Request scope attributes, Request parameters, and framework Context scope parameters:-

Application Scope Attribute

Assuming there's an attribute with name myApplicationAttribute in the Application scope.

<#if Application.myApplicationAttribute?exists>


< value="%{#application.myApplicationAttribute}" />

Session Scope Attribute

Assuming there's an attribute with name mySessionAttribute in the Session scope.

<#if Session.mySessionAttribute?exists>


< value="%{#session.mySessionAttribute}" />

Request Scope Attribute

Assuming there's an attribute with name 'myRequestAttribute' in the Request scope.

<#if Request.myRequestAttribute?exists>


< value="%{#request.myRequestAttribute}" />

Request Parameter

Assuming there's a request parameter myParameter (eg. http://host/myApp/myAction.action?myParameter=one).

<#if Parameters.myParameter?exists>


< value="%{#parameters.myParameter}" />

Context parameter

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



< value="%{#myContextParam}" />

Template Loading

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

  1. Web application
  2. 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.

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:

  1. Built-in variables
  2. Value stack
  3. Action context
  4. Request scope
  5. Session scope
  6. 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.

<@s.url id="url" value=""/>
Click <a xhref="${url}">here</a>!

The built-in variables that Struts-FreeMarker integration provides are:




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


The action most recently executed


The HttpServletResponse


Same as response


The HttpServletRequest


Same as request


The HttpSession


The ServletContext


The request's context path

FreeMarker configuration

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


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
<@s.if test="printName">
    < value="myBeanProperty" />

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
<#assign ex=JspTaglibs["/WEB-INF/example.tld"] />

<@ex.mytag text="hello" />

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 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.

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!
<@s.textarea rows=5 cols=40 />


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.freemarker.manager.classname = com.yourcompany.YourFreeMarkerManager

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
   <head>FreeMarker Example</head>
       <h1>Alternative Syntax</h1>
       [@s.if test="printName"]
          [ value="myBeanProperty" /]

(lightbulb) 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.

  • No labels