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 Struts group prefers FreeMarker to Velocity because FreeMarker has better error reporting , support for JSP tags, and slightly better featuresand supports JSP taglibs.. However, both are good alternatives to JSP.

...

Getting started with FreeMarker is as simple as ensuring all the dependencies are included in your project's classpath. This typically requires simply Typically, the only dependency is the {{freemarker.jar}. Other than that, struts-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:process your application's templates.

Code Block
xml
xml
titlestruts.
Code Block
xmlxml
<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
titletest-success.ftl
<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.

...

The following are ways to obtained Application scope attributes, Session scope attributes, Request scope attributes, Request parameters, and SAF framework Context scope parameters:-

...

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<@s.property value="%{#application.myApplicationAttribute}" />

...

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<@s.property value="%{#session.mySessionAttribute}" />

...

Code Block
<#if Request.myRequestAttribute?exists>
      ${Request.myRequestAttribute}
</#if>

or

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

...

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

or

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

...

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

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

or

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

Template Loading

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

...

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 WebWorkthe framework.

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

Note

This variable is currently NOT relative to the root of your webapp. We suggest placing the templates under WEB-INF anywayapplication.

Variable Resolution

In When using FreeMarker with the framework, variables are looked up in several different places, in this order:

  1. Built-in variables
  2. The value Value stack
  3. The action Action context
  4. Request scope
  5. Session scope
  6. Application scope

Note that the action context is looked up after the value stack. This means that you can reference the variable without the typical preceding has marker (#) like you would have to when using the JSP ww s:property tag. This is a nice convenience, though be careful because there is a small chance it could trip you up.

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

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

Name

Description

stack

The value stack itself, useful for calls like ${stack.findString('ognl expr')}

action

The action most recently executed

response

The HttpServletResponse

res

Same as response

request

The HttpServletRequest

req

Same as request

session

The HttpSession

application

The ServletContext

base

The request's context path

Tag Support

FreeMarker is a great template language because it has includes complete tag support. See the FreeMarker Tags documentation for information on how to use the generic Tags provided by WebWorkStruts. In addition to this, you can use any JSP tag, like so:

...

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 in web.xml documented as follows:-

Wiki Markup
{snippet:lang=xml|id=freemarkerSupport|url=action2struts2/apps/starter/src/main/webapp/WEB-INF/web.xml}

...

There are some advanced features that may be useful when building WebWork Struts applications with FreeMarker.

...

FreeMarker has built in support for formatting dates and numbers. The formatting rules are based on the locale associated with the action request, which is by default set in struts.properties but can be over-ridden using the I18n Interceptor. This is normally perfect for your needs, but it is important to remember that these formatting rules are handled by FreeMarker and not by WebWorkthe framework's Type Conversion support.

If you want WebWork the framework to handle the formatting according to the Type Conversion you have specified, you shouldn't use the normal ${...} syntax. Instead, you should use the property tag. The difference is that the property tag is specifically designed to take an OGNL expression, evaluate it, and then convert it to a String using any Type Conversion rules you have specified. The normal ${...} syntax will use a FreeMarker expression language, evaluate it, and then convert it to a String using the built in formatting rules.

(warning) The difference in how type conversion is handled under Freemarker This difference is subtle but important to understand.

Extending

Sometimes you may with to extend the framework's FreeMarker support provided with WebWork. The most common reason for doing this is that you wish to include your own Tags, such as those that you have extended from the built in WebWork Tags.. For example, you might want to extend the Struts tags that come bundled with the framework.

To extend the Freemarker support, develop a class that extends org.apache.struts2To do so, write a new class that extends com.opensymphony.webwork.views.freemarker.FreemarkerManager and overrides it as needed. Then add the following to , overriding methods as needed, and plugin the class through the struts.properties:

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

...

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=org.apache.struts.action2struts2.views.freemarker.StrutsBeanWrapper}

...