FreeMarker is a "Java-based template engine "; a generic tool to generate text output (anything from HTML to auto generated source code) based on templates.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.
Table of Contents | ||
---|---|---|
|
Configure your action to use the "freemarker" result type
The 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".
Code Block | ||||
---|---|---|---|---|
| ||||
<include file="struts-default.xml"/>
...
<action name="test" class="package.Test">
<result name="success" type="freemarker">/WEB-INF/views/testView.ftl</result>
</action>
...
|
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:
Code Block |
---|
Your name is: ${name} |
Property resolution
When a property is referenced in a FreeMarker template, the following scopes will be searched in order, until a value is found:
...
Note | ||
---|---|---|
| ||
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.
Code Block |
---|
<#if Application.myApplicationAttribute?exists>
${Application.myApplicationAttribute}
</#if>
|
or
Code Block |
---|
<@s.property value="%{#application.myApplicationAttribute}" />
|
Session Scope Attribute
Assuming there's an attribute with name mySessionAttribute
in the Session scope.
Code Block |
---|
<#if Session.mySessionAttribute?exists>
${Session.mySessionAttribute}
</#if>
|
or
Code Block |
---|
<@s.property value="%{#session.mySessionAttribute}" />
|
Request Scope Attribute
Assuming there's an attribute with name 'myRequestAttribute' in the Request scope.
Code Block |
---|
<#if Request.myRequestAttribute?exists>
${Request.myRequestAttribute}
</#if>
|
or
Code Block |
---|
<@s.property value="%{#request.myRequestAttribute}" />
|
Request Parameter
Assuming there's a request parameter myParameter (eg. http://host/myApp/myAction.action?myParameter=one).
Code Block |
---|
<#if Parameters.myParameter?exists>
${Parameters.myParameter}
</#if>
|
or
Code Block |
---|
<@s.property value="%{#parameters.myParameter}" />
|
Context parameter
Assuming there's a parameter with the name myContextParam in framework context.
Code Block |
---|
${stack.findValue('#myContextParam')}
|
or
Code Block |
---|
<@s.property value="%{#myContextParam}" />
|
Template Loading
The framework looks for FreeMarker templates in two locations (in this order):
- Web application
- 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.
Note |
---|
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 locally, on the template.
Note | ||
---|---|---|
| ||
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. |
Provided objects
The following variables are provided by Struts to the FreeMarker templates:
...
- This class contains useful methods to execute OGNL expressions against arbitrary objects, and a method to generate a select list using the <s:select> pattern. (i.e. taking the name of the list property, a listKey and listValue)
...
Variable Resolution
When using FreeMarker with the framework, variables are looked up in several different places, in this order:
- Built-in variables
- Value stack
- Action context
- Request scope
- Session scope
- 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.
Code Block | ||||
---|---|---|---|---|
| ||||
<@s.url id="url" value="http://www.yahoo.com"/>
Click <a xhref="${url}">here</a>!
|
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 |
...
FreeMarker configuration
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.
Code Block | ||
---|---|---|
| ||
default_encoding=ISO-8859-1 template_update_delay=5 locale=no_NO |
Tags
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:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<@s.if test="printName"> <@s.property value="myBeanProperty" /> </@s.if> |
...
To use JSP tags that are not part of Struts you have to import the tld. There are two ways of making a tld available to FreeMarker templates:to:
1. Add JspSupportSerlvet to web.xml
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<servlet>
<servlet-name>JspSupportServlet</servlet-name>
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
|
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
...
.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<#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 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.
Note |
---|
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:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<@s.textarea rows=5 cols=40 />
|
Extending
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:
Code Block | ||||
---|---|---|---|---|
| ||||
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.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
[#ftl] <html> <head>FreeMarker Example</head> <body> <h1>Alternative Syntax</h1> [@s.if test="printName"] [@s.property value="myBeanProperty" /] [/@s.if] </body> </html> |
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:
...
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.
FreeMarker links
...