Versions Compared

Key

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

...

Note

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, action-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:

...

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.

...

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

Session Scope Attribute

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

...

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

Request Scope Attribute

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

...

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

Request Parameter

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

...

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

...

Context parameter

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

...

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

Template Loading

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

...

Note

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:

...

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:

...

Wiki Markup
{snippet:lang=xml|id=freemarkerSupport|url=action2/apps/starter/src/main/webapp/WEB-INF/web.xml}

Tips and Tricks

There are some advanced features that may be useful when building WebWork 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 action.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.

...

Code Block
none
none
struts.freemarker.manager.classname = com.yourcompany.YourFreeMarkerManager

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:

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=org.apache.struts.action2.views.freemarker.StrutsBeanWrapper}

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.

Next: Freemarker Tags