This page last changed on Nov 30, 2004 by jcarreira.

Freemarker Support in WebWork

Freemarker views can be rendered either via the webwork result type freemarker, or by using the dispatcher result type in conjunction Webwork's FreemarkerServlet.

This document will focus on using the freemarker result since it is the recommended approach. An section follows to show how to use the FreemarkerServlet.

Configure your action to use the freemarker result type

The freemarker result type is defined in webwork-default.xml, so normally you just include it, and define your resutls to use type="freemarker".

<include file="webwork-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 ${name} will result in stack.findValue("name"), which generaly results in action.getName() being executed.

A search process is used to resolve the variable, searching the following scopes in order, until a value is found :
  • freemarker variables
  • value stack
  • request attributes
  • session attributes
  • servlet context attributes

Objects in the Context

The following variables exist in the freemarer views

  • req - the current HttpServletRequest
  • res - the current HttpServletResponse
  • stack - the current OgnlValueStack
  • ognl - the OgnlTool instance
    • This class contains useful methods to execute OGNL expressions against arbitary objects, and a method to generate a select list using the <ww:select> pattern. (i.e. taking the name of the list property, a listKey and listValue)
  • webwork - an instance of FreemarkerWebWorkUtil
  • action - the current WebWork action
  • exception - optional the Exception instance, if the view is a JSP exception or Servlet exception view

FreeMarker configuration with recent (post 2.1) releases

To configure the freemarker engine that webwork uses, just add a file to the classpath. The supported properties are those that the freemarker Configuration object expects - see the freemarker documentation for these. These properties are used for both the freemarker result type, and the webwork provided FreemarkerServlet.

Using webwork 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 ww=JspTaglibs["/WEB-INF/webwork.tld"] />

<@ww.form method="'post'" name="'inputform'" action="'save.action'" >
    <@ww.hidden name="'id'" />
    <@ww.textarea label="'Details'" name="'details'" rows=5 cols=40 />
    <@ww.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.

Using the FreemarkerServlet

The FreemarkerServlet provided in the freemarker.jar will work out of the box however it won't provide any webwork specific functionality such as the context variables, property resoloution etc. Therefore webwork provides its own servlet to provide this integration.

Register the FreemarkerServlet in web.xml

To use freemarker as a view engine, the webwork2 FreemarkerServlet needs to be configured, and mapped to the file extension that you use for your templates.



Configure Actions to use this servlet (xwork.xml configuration)

To use the freemarker view, just use the dispatcher result type, and specify the location to the template file.
<action name="test" class="package.Test">
  <result name="success" type="dispatcher">/WEB-INF/views/testView.ftl</result>

Extending the servlet

NOTE: these docs need to be revised, since the FreemarkerServlet has changed since they were written.

Please refer to the freemarker site for details about the base freemarker servlet.

Be carfeul when subclassing com.opensymphony.webwork.views.freemarker.FreemarkerServlet when overriding
protected TemplateModel createModel(
    ObjectWrapper wrapper, 
    ServletContext servletContext, 
    HttpServletRequest request, 
    HttpServletResponse response)

Please call super.createModel(...) and wrap it with a new model to avoid problems with action property resoloution.

Document generated by Confluence on Dec 14, 2004 16:37