...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<action name="test" class="com.acme.TestAction">
<result name="success" type="freemarker">test-success.ftl</result>
</action>
|
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<html>
<head>
<title>Hello</title>
</head>
<body>
Hello, ${name}
</body>
</html>
|
...
Assuming there's an attribute with name myApplicationAttribute
in the Application scope.
Code Block |
---|
<#if Application.myApplicationAttribute?exists>
${Application.myApplicationAttribute}
</#if>
|
or
Code Block |
---|
<@s.property value="%{#application.myApplicationAttribute}" />
|
...
Assuming there's an attribute with name mySessionAttribute
in the Session scope.
Code Block |
---|
<#if Session.mySessionAttribute?exists>
${Session.mySessionAttribute}
</#if>
|
or
Code Block |
---|
<@s.property value="%{#session.mySessionAttribute}" />
|
...
Assuming there's an attribute with name 'myRequestAttribute' in the Request scope.
Code Block |
---|
<#if Request.myRequestAttribute?exists>
${Request.myRequestAttribute}
</#if>
|
or
Code Block |
---|
<@s.property value="%{#request.myRequestAttribute}" />
|
...
Assuming there's a request parameter myParameter (eg. http://host/myApp/myAction.action?myParameter=one).
Code Block |
---|
<#if Parameters.myParameter?exists>
${Parameters.myParameter}
</#if>
|
or
Code Block |
---|
<@s.property value="%{#parameters.myParameter}" />
|
...
Assuming there's a parameter with the name myContextParam in framework context.
Code Block |
---|
${stack.findValue('#myContextParam')}
|
or
Code Block |
---|
<@s.property value="%{#myContextParam}" />
|
...
Note that the action context is looked up after the value stack. This means that you can reference the variable without the typical preceding has marker (#) like you would have to when using the JSP s:property
tag. This is a nice convenience, though be careful because there is a small chance it could trip you up.
Code Block | ||||
---|---|---|---|---|
| ||||
<@s.url id="url" value="http://www.yahoo.com"/>
Click <a xhref="${url}">here</a>!
|
...
FreeMarker includes complete tag support. See the FreeMarker Tags documentation for information on how to use the generic Struts Tags provided by Struts. In addition to this, you can use any JSP tag, like so:
Code Block | ||||
---|---|---|---|---|
| ||||
<#assign mytag=JspTaglibs["/WEB-INF/mytag.tld"]>
<@mytag.tagx attribute1="some ${value}"/>
|
...
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>
|
...
To extend the Freemarker support, develop a class that extends org.apache.struts2.views.freemarker.FreemarkerManager
, overriding methods as needed, and plugin the class through the struts.properties:
Code Block | ||||
---|---|---|---|---|
| ||||
struts.freemarker.manager.classname = com.yourcompany.YourFreeMarkerManager
|
...
Setting devMode
to true will disable cache and updateDelay immediately, but you can explicit specify these constants to enable cache even in devMode
, see devMode
Incompatible Improvements
By default Struts is using FreeMarker in way to be as much as possible backward compatible but if you need to enable new features you can do it via freemarker.properties
by defining incompatible improvements settings, ie.:
Code Block | ||
---|---|---|
| ||
incompatible_improvements=2.3.22 |