Home > Guides > User Guide > Using Freemarker Templates |
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.
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".
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:
The following are ways to obtained Application scope attributes, Session scope attributes, Request scope attributes, Request parameters, and framework Context scope parameters:-
Assuming there's an attribute with name myApplicationAttribute
in the Application scope.
or
Assuming there's an attribute with name mySessionAttribute
in the Session scope.
or
Assuming there's an attribute with name 'myRequestAttribute' in the Request scope.
or
Assuming there's a request parameter myParameter (eg. http://host/myApp/myAction.action?myParameter=one).
or
Assuming there's a parameter with the name myContextParam in framework context.
or
The framework looks for FreeMarker templates in two locations (in this order):
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.
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.
When using FreeMarker with the framework, variables are looked up in several different places, in this order:
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:
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 |
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.
Tags distributed with Struts are automatically made available to FreeMarker templates. To use any tag add "@s.
" in front of the tag name. Like:
To use JSP tags that are not part of Struts you have to:
1. Add 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.
There are some advanced features that may be useful when building Struts applications with FreeMarker.
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.
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:
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 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.
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.