Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

The framework utilizes FreeMarker because

...

the engine includes strong error reporting, built-in internationalization and powerful macro libraries.

Support is also included for Velocity templates. For a comparison of Velocity vs FreeMarker see here.

...

Getting started with FreeMarker is as simple as ensuring all the dependencies are included in your project's classpath. Typically, the only dependency is the {{freemarker.jar}. Other than that, struts-default.xml already configures the FreeMarker Result needed to process your application's templates.

...

...

Then in test-success.ftl:

...

...

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.

...

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

...

...

or

...

Session Scope Attribute

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

...

...

or

...

Request Scope Attribute

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

...

...

or

...

Request Parameter

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

...

or

...

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):

...

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.

...

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 built-in variables that Struts-FreeMarker integration provides are:

...

FreeMarker includes complete tag support. See the FreeMarker Tags documentation for information on how to use the generic Struts Tags provided by Struts. In addition to this, you can use any JSP tag, like so:

...

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 in web.xml:

...

Tips and Tricks

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

...

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:

...

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:

...

INLINE{snippet:id=javadoc|javadoc=true|url=org.apache.struts2.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.

Cache

You can enable FreeMarker cache mechanism by specifying below options in struts.xml:

  • <constant name="struts.freemarker.templatesCache.updateDelay" value="1800" /> - default update cache interval (5 seconds)
  • <constant name="struts.freemarker.templatesCache" value="true" /> - *DEPRECATED* this option will use a internal ConcurrentHashMap in FreemarkerTemplateEngine but not freemarker native cache

Setting devMode to true will disable cache and updateDelay immediately, but you can explicit specify these constants to enable cache even in devMode, see devMode

Incompatible Improvements

By default Struts is using FreeMarker in way to be backward compatible as much as possible but if you need to enable new features you can do it via freemarker.properties by defining incompatible improvements settings, ie.:

...

You can also pass this setting via ServletContext <init-param/> (since Struts 2.5.13):

...


This can impact your freemarker powered pages and Struts tags as well, so please careful test this change.

Next: Freemarker FreeMarker Tags