...
For example, in JSP you might create a form using Struts tags.
Code Block | |||||||
---|---|---|---|---|---|---|---|
| |||||||
<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 | |||||||
---|---|---|---|---|---|---|---|
| |||||||
<@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.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<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 | |||||||
---|---|---|---|---|---|---|---|
| |||||||
<@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.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<@s.form action="updatePerson"> <@s.textfield label="First name" name="firstName" description="..."/> <@s.submit value="Update"/> </@s.form> |
...
Code Block | ||||
---|---|---|---|---|
| ||||
<@s.select label="Foo label - ${foo}" name="${name}" list=[1, 2, 3]/> |
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 {{ Wiki Markup 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 | ||||||
---|---|---|---|---|---|---|
| ||||||
<servlet> <servlet-name>JspSupportServlet</servlet-name> <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> |
...