Versions Compared

Key

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

...

Note

FreeMarker is very similar to Velocity, as both are template languages that can be used outside of a Servlet container. The WebWork team recommends FreeMarker over Velocity simply because FreeMarker has better error reporting, support for JSP tags, and slightly better features. However, both are good alternatives to JSP.

 

Getting Started

Getting started with FreeMarker is as simple as ensuring all the dependencies are included in your project's classpath. This typically requires simply freemarker.jar. Other than that, webwork-default.xml already configures the FreeMarker Result needed to map your actions to your templates. You may now try out the following xwork.xml configuration:

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.

...

This ordering makes it ideal for providing templates inside a fully built jar, but allowing for overrides of those templates to be defined in your web application. In fact, this is how you can override the default UI tags and Form Tags included with WebWork.

In addition, you can specify a location (directory on your file system) thru templatePath context variable (in your web.xml). If variable is specified, the content of the directory it points to will be searched first.

Variable Resolution

In FreeMarker, variables are looked up in several different places, in this order:

...

Code Block
xml
xml
<@ww.url id="url" value="http://www.yahoo.com"/>
Click <a hrefxhref="${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.