Home > Guides > Core Developers Guide > Result Types > FreeMarker Result > Freemarker Support |
Freemarker views can be rendered using the webwork result type freemarker
.
freemarker
result typeThe freemarker
result type is defined in struts-default.xml
, so normally you just include it, and define your resuts to use type="freemarker"
.
Your action properties are automatically resolved - just like in a velocity view.
for example ${name
} will result in stack.findValue("name")
, which generaly results in action.getName()
being executed.
A search process is used to resolve the variable, searching the following scopes in order, until a value is found :
The following variables exist in the FreeMarker views
req
- the current HttpServletRequestres
- the current HttpServletResponsestack
- the current OgnlValueStackognl
- the OgnlTool instance
struts
- an instance of StrutsBeanWrapperaction
- the current Struts actionexception
- optional the Exception instance, if the view is a JSP exception or Servlet exception viewTo configure the freemarker engine that Struts uses, just add a file freemarker.properties
to the classpath. The supported properties are those that the Freemarker Configuration object expects - see the Freemarker documentation for these.
Freemarker has builtin support for using any JSP taglib. You can use JSP taglibs in FreeMarker even if
a) your servlet container has no support for JSP, or
b) you didn't specify the taglib in your web.xml - note how in the example below we refer to the taglib by its webapp-absolute URL, so no configuration in web.xml is needed.
NOTE : numeric properties for tags MUST be numbers, not strings. as in the rows and cols properties above. if you use cols="40" you will receive an exception. Other than that, the freemarker tag container behaves as you would expect.
You can specify dynamic attributes with Struts 2 tags like this:
or like this:
and for both case, it will be parsed into:
You can also use OGNL expressions with dynamic tags like below: