Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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
XML
XML
<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
titleBe Careful

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
titlefreemarker.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
titleUsing 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
titleUsing 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
Do not quote non string values in tag attributes!
XML
XML
titleDo not quote non string values in tag attributes!
<@s.textarea label="'Details'" name="'details'" rows=5 cols=40 />