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.
Table of Contents | ||
---|---|---|
|
Configure your action to use the "freemarker" result type
...
Code Block |
---|
Your name is: ${name} |
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
...
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.
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. |
Variable Resolution
When using FreeMarker with the framework, variables are looked up in several different places, in this order:
...
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> |
Using JSP tags
To use JSP tags that are not part of Struts you have to:
...
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.
...
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.