FreeMarker is a "template engine"; , a generic tool to generate text output (anything from HTML to auto generated source code) based on templates.
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>
...
|
...
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. |
...
Code Block |
---|
title | freemarker.properties example |
---|
|
default_encoding=ISO-8859-1
template_update_delay=5
locale=no_NO
|
...
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 |
---|
| HTML |
---|
| HTML |
---|
title | Using Struts tags on FreeMarker templates |
---|
|
<@s.if test="printName">
<@s.property value="myBeanProperty" />
</@s.if>
|
...
- Declare the tld on web.xml
- Use FreeMarker's "
assign
" directive. When using the "assign
" directive, provide the full absolute path to the tld file, like:
Code Block |
---|
| XMLHTMLXML |
---|
| HTML |
---|
title | Using JSP tags on FreeMarker templates |
---|
|
<#assign ex=JspTaglibs["/WEB-INF/example.tld"] />
<@ex.mytag text="hello" />
|
...
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 |
---|
| XML |
---|
| XML |
---|
title | Do not quote non string values in tag attributes! | Do not quote non string values in tag attributes! |
---|
|
<@s.textarea label="'Details'" name="'details'" rows=5 cols=40 />
|
FreeMarker links