Versions Compared

Key

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

...

Code Block
xml
xml
<action name="test" class="com.acme.TestAction">
    <result name="success" type="freemarker">test-success.ftl</result>
</action>

 

Then in test-success.ftl:

Code Block
xml
xml
<html>
<head>
    <title>Hello</title>
</head>
<body>

Hello, ${name}

</body>
</html>

...

Where name is a property on your action. That's it! Read the rest of this document for details on how templates are loaded, variables are resolved, and tags can be used.

Servlet / JSP Scoped Objects

The following are ways to obtained Application scope attributes, Session scope attributes, Request scope attributes, Request parameters and SAF 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

  <@saf.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

  <@saf.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

   <@saf.property value="%{#request.myRequestAttribute}" />

Request Parameter

Assuming there's a request parameter myParameter (eg. http://host/myApp/myAction.action?myParameter=oneImage Added).

Code Block

<#if Parameters.myParameter?exists>
     ${Parameters.myParameter}
</#if>

or

Code Block

  <@saf.property value="%{#parameters.myParameter}" />

SAF Context parameter

Assuming there's a parameter with the name myContextParam in SAF context.

Code Block

   ${stack.findValue('#myContextParam')}

or

Code Block

  <@saf.property value="%{#myContextParam}" />

Template Loading

WebWork looks for FreeMarker templates in two locations (in this order):

...

Code Block
xml
xml
<@ww.url id="url" value="http://www.yahoo.com"/>
Click <a xhref="${url}">here</a>!

 

The built-in variables that WebWork-FreeMarker integration provides are:

...

Code Block
xml
xml
<#assign mytag=JspTaglibs["/WEB-INF/mytag.tld"]>
<@mytag.tagx attribute1="some ${value}"/>

 

Where mytag.tld is the JSP Tag Library Definition file for your tag library. Note: in order to use this support in FreeMarker, you must enable the JSPSupportServlet documented in web.xml 2.1.x compatibility.

...

Code Block
none
none
webwork.freemarker.manager.classname = com.yourcompany.YourFreeMarkerManager

...

ObjectWrapper Settings

Once you get familiar with FreeMarker, you will find certain subtletieswith it that may become frustrating. The most common thing you'll likely run in to is the BeansWrapper provided by FreeMarker. If you don't know what this is, don't worry. However, if you do, know this:

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=com.opensymphony.webwork.views.freemarker.WebWorkBeanWrapper}

...

Syntax Notes

As of FreeMarker 2.3.4, an alternative syntax is supported. This alternative syntax is great if you find that your IDE (especially IntelliJ IDEA) makes it difficult to work with the default syntax. You can read more about this syntax here.