Versions Compared

Key

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

Update formatting and nomenclature

FreeMarker tags are extensions of the generic Tags provided by WebWorkthe framework. You can get started almost immediately just by simply knowing the generic structure in which the tags can be accessed: <@ww<@saf.xxx> ...</@ww@saf.xxx>, where xxx is any of the tags supported by WebWorkthe framework.

Syntax

For example, in JSP you might create a form like so:using SAF tags.

Code Block
xml
xml
titleJSP Form
<ww<saf:form action="updatePerson">
    <ww<saf:textfield label="First name" name="firstName"/>
    <ww<saf:submit value="Update"/>
</wwsaf:form>

In FreeMarker the same form is built like so:can also be built using SAF tags.

Code Block
xml
xml
<@ww<@saf.form action="updatePerson">
    <@ww<@saf.textfield label="First name" name="firstName"/>
    <@ww<@saf.submit value="Update"/>    
</@ww@saf.form>

While this covers almost all know need to know for for FreeMarker tags, there are a few other advanced features you should read about, specifically with how attributes and parameters work together, and how attribute types (String, List, etc) can affect the tag behavior.

Attributes and Parameters

Unlike older versions of JSP (in which the JSP Tags are based), FreeMarker allows for dynamic attributes, much like JSP 2.0. What this means is that you You can supply attributes to the tags that the tag doesn't even explicitedly support. Those attributes that cannot be applied directly to the tag object will instead be set on to the tag's general-purpose parameters map. Map.

Suppose we wanted to build an URL in a JSP. The URL needs to take an arbitary parameter to the query string, that (being arbitary) isn't specified on the URL tag. In a JSP, we'd have to use the url and param tags together.For example, suppose you have the following code in JSP:

Code Block
xml
xml
titleCreating a URL with a query string (JSP)

<saf
<ww:url value="somePage">
    <ww<saf:param name="personId" value="%{personId}"/>
</wwsaf:url>

In FreeMarker, you can simplify this as:we can pass the arbitrary parameter directly and create the URL in one simple statement.

Code Block
xml
xml
titleCreating a URL with a query string (FTL)

<@saf
<@ww.url value="somePage" personId="${personId}"/>

Using inline attributes with templates

Suppose In addition to being able to replace cases where you might use the param tag, you can also use this functionality when building additional templates or themes for your Form Tags. For example, suppose you created a "three column" theme to replace the typical two column theme (xhtml). You might want an additional parameter to display in the third column called "description". Your form can be:Using FreeMarker, you can just pop the description attribute into the textfield tag, no fuss, no muss.

Code Block
xml
xml
titlePassing an attribute to the template
<@ww<@saf.form action="updatePerson">
    <@ww<@saf.textfield label="First name" name="firstName" description="..."/>
    <@ww<@saf.submit value="Update"/>    
</@ww@saf.form>

And then in your In the new template you can refer to , the description using is referenced via the parameters Map: "${parameters.description}".

...

{tip:title=Advanced uses of the param tag}
For simple cases, inline attributes are much easier to use than the param} tag. But, the {{param} tag is more flexible than inline attributes for advanced use cases. For example, {{param can take the entire body of the

...

tag and apply that as the value attribute.

Tip

Attribute Types

Remember that all tag attributes must first be set as Strings – they are then later evaluated (using OGNL) to a different type, such as List, int, or boolean. This generally works just fine, but it can be limiting when using FreeMarker which provides more advanced ways to apply attributes. Suppose the following example:

Code Block
xml
xml
<@ww<@saf.select label="Foo label - ${foo}" name="${name}" list="%{{1, 2, 3}}"/>

What will happen here is that each attribute will be evaluated to a string String as best it can. This may involve calling the toString() method on the internal FreeMarker objects in the hash. In this case, all objects will end up being exactly what you would expect. Then, when the tag runs, the list attribute will be converted from a String to a List using OGNL's advanced collection support.

But suppose you wish to use FreeMarker's list or hash support instead? You can do this:

Code Block
xml
xml
<@ww<@saf.select label="Foo label - ${foo}" name="${name}" list={1, 2, 3}/>

Wiki Markup
Notice that the list attribute no longer has quotes around it. Now it will come in to the tag as an object that can't easily be converted to a String. Normally, the tag would just call {{toString()}}, which would return "\[1, 2, 3\]" and be unable to be converted back to a List by OGNL. Rather than go through all this back and forth anyway, the frameworks's FreeMarker tag support within WebWork will recognize collections and not pass them through the normal tag attribute. Instead, the butframework insteadwill set them directly in the *{{parameters*}} mapMap, ready to be consumed by the template.

In the end, everything tends to do what you would expect, but it can help to understand the difference of when OGNL is being used and when it isn't, and how attribute types get converted.

JSP Tag Support

While WebWork the framework provides native FreeMarker Tags, you might wish to use other third-party tags that are only available for JSP. Fortunately, FreeMarker has the ability to run JSP tags. To do so, you must include the JspSupportServlet outlined in the application's web.xml 2.1.x compatibility, as this allows the FreeMarker integration to get access to the required objects needed to emulate a JSP taglib container.

Code Block
xml
xml
titleAdding JspSupportSerlvet to web.xml

<servlet>
    <servlet-name>JspSupportServlet</servlet-name>
    <servlet-class>com.opensymphony.webwork.views.JspSupportServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

Once you've done that, you can simply add something like this in your templates:

...