Fork me on GitHub
<< back to Core Developers Guide Edit on GitHub

Freemarker Support

Freemarker views can be rendered using the webwork result type



Configure your action to use the freemarker result type



result type is defined in


, so normally you just include it, and define your resuts to use



<include file="struts-default.xml"/>
<action name="test" class="package.Test">
  <result name="success" type="freemarker">/WEB-INF/views/testView.ftl</result>

Property Resoloution

Your action properties are automatically resolved - just like in a velocity view.

for example


} will result in


, which generaly results in


being executed.

A search process is used to resolve the variable, searching the following scopes in order, until a value is found :

Objects in the Context

The following variables exist in the FreeMarker views


- the current HttpServletRequest


- the current HttpServletResponse


- the current OgnlValueStack


- the OgnlTool instance


- an instance of StrutsBeanWrapper


- the current Struts action


- optional the Exception instance, if the view is a JSP exception or Servlet exception view

FreeMarker configuration with recent releases

To configure the freemarker engine that Struts uses, just add a file

to the classpath. The supported properties are those that the Freemarker Configuration object expects - see the Freemarker documentation^[,%20java.lang.String)] for these.


Using struts UI tags - or any JSP Tag Library

Freemarker has builtin support for using any JSP taglib. You can use JSP taglibs in FreeMarker even if a) your servlet container has no support for JSP, or b) you didn’t specify the taglib in your web.xml - note how in the example below we refer to the taglib by its webapp-absolute URL, so no configuration in web.xml is needed.

<#assign s=JspTaglibs["/WEB-INF/struts.tld"] />

<@s.form method="'post'" name="'inputform'" action="'save.action'" >
    <@s.hidden name="'id'" />
    <@s.textarea label="'Details'" name="'details'" rows=5 cols=40 />
    <@s.submit value="'Save'" align="center" />

NOTE : numeric properties for tags MUST be numbers, not strings. as in the rows and cols properties above. if you use cols=”40” you will receive an exception. Other than that, the freemarker tag container behaves as you would expect.

Dynamic attributes support

You can specify dynamic attributes with Struts 2 tags like this:

<@s.textfield name="test" dynamicAttributes={"placeholder":"input","foo":"bar"}/>

or like this:

<@s.textfield name="test" placeholder="input" foo="bar"/>

and for both case, it will be parsed into:

<input type="text" name="test" value="" id="test" placeholder="input" foo="bar"/>

You can also use OGNL expressions with dynamic tags like below:

<@s.textfield name="test" placeholder="input" foo="checked: %{bar}"/>

When using attributes with hyphens, use the below syntax (you can also leave the single quotes from false if you want)

<@s.form dynamicAttributes={'data-ajax':'false'}>