Versions Compared

Key

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

...

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

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

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

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

...

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.

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

In FreeMarker, we can pass the arbitrary parameter directly and create the URL in one simple statement.

Code Block
xmlxml
titleCreating a URL with a query string (FTL)
xml
<@s.url value="somePage" personId="${personId}"/>

...

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". Using FreeMarker, you can just pop the description attribute into the textfield tag, no fuss, no muss.

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

...

Code Block
xml
xml
<@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.

xml
Code Block
xml
titleAdding JspSupportSerlvet to web.xml
xml
<servlet>
    <servlet-name>JspSupportServlet</servlet-name>
    <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

...