Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

FreeMarker tags are extensions of the generic Struts Tags provided by the framework. You can jump right in just by knowing the generic structure in which the tags can be accessed: <@saf<@s.xxx> tag> ...</@saf@s.xxx>tag>, where xxx tag is any of the tags supported by the framework.

For example, in JSP you might create a form using SAF Struts tags.

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

In FreeMarker the same form can also be built using SAF Struts tags.

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

But, wait there's more!

Aside from doing everything that the JSP tags do, the FTL tags boast some advanced additional features that you can use to make your pages even easier to code. You can even invoke third-party JSP taglibs as if there were native FTL tags.

...

Code Block
xml
xml
titleCreating a URL with a query string (JSP)
<saf<s:url value="somePage">
    <saf<s:param name="personId" value="%{personId}"/>
</safs:url>

In FreeMarker, 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<@s.url value="somePage" personId="${personId}"/>

...

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

In the new template, the description is referenced via the parameters Map: "${parameters.description}".

...

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
<@saf<@s.select label="Foo label - ${foo}" name="${name}" list="%{{1, 2, 3}}"/>

...

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

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

Wiki MarkupNotice 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, the frameworks's FreeMarker tag support will recognize collections and not pass them through the normal tag attribute. Instead, the framework will set them directly in the {{parameters}} Map, 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.

...

While 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 in the application's web.xml, 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>comclass>org.opensymphonyapache.webworkstruts2.views.JspSupportServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

...

Code Block
xml
xml
<#assign cewolf=JspTaglibs["/WEB-INF/cewolf.tld"] />
...
<@cewold.xxx ... />

Next: Velocity