Versions Compared

Key

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

...

Tags distributed with Struts are automatically made available to FreeMarker templates. To use any tag add "@s." in front of the tag name. Like:

Code Block
HTMLHTML
titleUsing Struts tags on FreeMarker templates
HTML
<@s.if test="printName">
    <@s.property value="myBeanProperty" />
</@s.if>

...

1. Add JspSupportSerlvet to web.xml

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>

2. Declare the tld on web.xml or use FreeMarker's "assign" directive. When using the "assign" directive, provide the absolute path to the tld file.

HTML
Code Block
HTML
titleUsing JSP tags on FreeMarker templates
HTML
<#assign ex=JspTaglibs["/WEB-INF/example.tld"] />

<@ex.mytag text="hello" />

...

In FreeMarker it is incorrect to quote non string values. If a value is quoted, then an string will be passed, instead of the expected object, causing an exception. For example, the "textarea" tag expects the attributes "rows" and "cols" of type Integer:

Code Block
XMLXML
titleDo not quote non string values in tag attributes!
XML
<@s.textarea rows=5 cols=40 />

...

FreeMarker alternative syntax

...

FreeMarker by default uses the "<#directive />" syntax. FreeMarker supports an alternative syntax, where [ and ] are used instead of < and >. To enable the alternative syntax, add \ [#ftl\] at the beginning of the template. The alternative syntax makes it easier to differentiate between FreeMarker directives, and JSP or HTML tags.

Code Block
XMLXML
titleUse alternative syntax
XML
[#ftl]
<html>
   <head>FreeMarker Example</head>
    
   <body>
       <h1>Alternative Syntax</h1>
       [@s.if test="printName"]
          [@s.property value="myBeanProperty" /]
       [/@s.if]
   </body>
</html>

...