...
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 | |||||||
---|---|---|---|---|---|---|---|
| |||||||
<@s.if test="printName"> <@s.property value="myBeanProperty" /> </@s.if> |
...
1. Add JspSupportSerlvet to web.xml
Code Block | |||||||
---|---|---|---|---|---|---|---|
| |||||||
<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.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<#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 | ||||||
---|---|---|---|---|---|---|
| ||||||
<@s.textarea rows=5 cols=40 /> |
...
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
[#ftl] <html> <head>FreeMarker Example</head> <body> <h1>Alternative Syntax</h1> [@s.if test="printName"] [@s.property value="myBeanProperty" /] [/@s.if] </body> </html> |
...