FreeMarker is a template Java template language 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.

For more information on FreeMarker itself, please visit the FreeMarker website.

FreeMarker is very similar to Velocity, as both are template languages that can be used outside of a Servlet container. The WebWork team recommends FreeMarker over Velocity simply because FreeMarker has better error reporting, support for JSP tags, and slightly better features. However, both are good alternatives to JSP.

 

Getting Started

Getting started with FreeMarker is as simple as ensuring all the dependencies are included in your project's classpath. This typically requires simply freemarker.jar. Other than that, webwork-default.xml already configures the FreeMarker Result needed to map your actions to your templates. You may now try out the following xwork.xml configuration:

<action name="test" class="com.acme.TestAction">
    <result name="success" type="freemarker">test-success.ftl</result>
</action>

 

Then in test-success.ftl:

<html>
<head>
    <title>Hello</title>
</head>
<body>

Hello, ${name}

</body>
</html>

 

Where name is a property on your action. That's it! Read the rest of this document for details on how templates are loaded, variables are resolved, and tags can be used.

Servlet / JSP Scoped Objects

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

Application Scope Attribute

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

<#if Application.myApplicationAttribute?exists>
     ${Application.myApplicationAttribute}
</#if>

or

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

Session Scope Attribute

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

<#if Session.mySessionAttribute?exists>
     ${Session.mySessionAttribute}
</#if>

or

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

Request Scope Attribute

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

<#if Request.myRequestAttribute?exists>
      ${Request.myRequestAttribute}
</#if>

or

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

Request Parameter

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

<#if Parameters.myParameter?exists>
     ${Parameters.myParameter}
</#if>

or

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

Context parameter

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

${stack.findValue('#myContextParam')}

or

<@saf.property value="%{#myContextParam}" />

Template Loading

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

In addition, you can specify a location (directory on your file system) through the 'templatePath' or 'TemplatePath' context variable (in your web.xml). If 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 webapp. We suggest placing the templates under WEB-INF anyway

Variable Resolution

In FreeMarker, variables are looked up in several different places, in this order:

  1. Built-in variables
  2. The value stack
  3. The 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 ww:property tag. This is a nice convenience, though be careful because there is a small chance it could trip you up.

<@ww.url id="url" value="http://www.yahoo.com"/>
Click <a xhref="${url}">here</a>!

 

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

Name Description
stack 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

Tag Support

FreeMarker is a great template language because it has complete tag support. See the FreeMarker Tags documentation for information on how to use the generic Tags provided by WebWork. In addition to this, you can use any JSP tag, like so:

<#assign mytag=JspTaglibs["/WEB-INF/mytag.tld"]>
<@mytag.tagx attribute1="some ${value}"/>

 

Where mytag.tld is the JSP Tag Library Definition file for your tag library. Note: in order to use this support in FreeMarker, you must enable the JSPSupportServlet documented in web.xml 2.1.x compatibility.

Tips and Tricks

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

Exception Handling

By default when an exception occurs a error report is printed. You can change the handler to meet your environment's needs, such as having it ignore errors or present a friendly error page.

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 webwork.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 WebWork's Type Conversion support.

If you want WebWork 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 Conversionrules 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. This difference is subtle but important to understand.

Extending

Sometimes you may with to extend the FreeMarker support provided with WebWork. The most common reason for doing this is that you wish to include your own Tags, such as those that you have extended from the built in WebWork Tags.

To do so, write a new class that extends com.opensymphony.webwork.views.freemarker.FreemarkerManager and overrides it as needed. Then add the following to webwork.properties:

webwork.freemarker.manager.classname = com.yourcompany.YourFreeMarkerManager

The custom Freemarker Manager could also be instatiated using IoC container like Spring. To learn how to allow IoC containers to instantiate WebWork's component (eg. Action, Interceptor etc.) have a look at here typically

For example if we have the following Spring beans xml configuration :-

<beans ...>
   <bean name="CustomFreemarkerManager" class="com.yourcompany.YourFreeMarkerManager">
     ...
   </bean>
 <beans>

The webwork.properties file would look something like :-

# turn on Spring integration
webwork.objectFactory=spring
webwork.freemarker.manager.classname = CustomFreemarkerManager

ObjectWrapper Settings

Once you get familiar with FreeMarker, you will find certain subtletieswith it that may become frustrating. The most common thing you'll likely run in to is the BeansWrapper provided by FreeMarker. If you don't know what this is, don't worry. However, if you do, know this:

The WebWorkBeanWrapper extends the default FreeMarker BeansWrapper and provides almost no change in functionality, except for how it handles maps. Normally, FreeMarker has two modes of operation: either support for friendly map built-ins (?keys, ?values, etc) but only support for String keys; OR no special built-in support (ie: ?keys returns the methods on the map instead of the keys) but support for String and non-String keys alike. WebWork provides an alternative implementation that gives us the best of both worlds.

It is possible that this special behavior may be confusing or can cause problems. Therefore, you can set the webwork.freemarker.wrapper.altMap property in webwork.properties to false, allowing the normal BeansWrapper logic to take place instead.

 

Syntax Notes

As of FreeMarker 2.3.4, an alternative syntax is supported. This alternative syntax is great if you find that your IDE (especially IntelliJ IDEA) makes it difficult to work with the default syntax. You can read more about this syntax here.