...
Next you will need to configure the FreeMarker servlet. The following is a minimal configuration that can be added to your web.xml. You may wish to consult FreeMarker's documentation on using FreeMarker with servlets for more details and configuration options.
Code Block | ||||
---|---|---|---|---|
| ||||
<servlet> <servlet-name>Freemarker</servlet-name> <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class> <init-param> <param-name>TemplatePath</param-name> <param-value>/</param-value> </init-param> <init-param> <param-name>template_update_delay</param-name> <param-value>0</param-value> <!-- 0 is for dev only! Use higher value otherwise. --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Freemarker</servlet-name> <url-pattern>*.ftl</url-pattern> </servlet-mapping> |
...
The last step in configuring FreeMarker is to ensure that the Stripes Filter filters requests that are made directly to FreeMarker templates. This step is optional, but highly recommended. If you never (and I mean never) allow navigation directly to FreeMarker templates (i.e. navigation is always through ActionBeans first and you always forward, never redirect to views) then this is not necessary.
Code Block | ||||
---|---|---|---|---|
| ||||
<filter-mapping> <filter-name>StripesFilter</filter-name> <servlet-name>Freemarker</servlet-name> <dispatcher>REQUEST</dispatcher> </filter-mapping> |
...
Once FreeMarker is installed and configured you can begin using it right away. The main part of Stripes that interacts heavily with the view tier is the tag library. Stripes does not provide an alternate macro library for use with FreeMarker, but thanks to FreeMarker's embedded lightweight JSP tag container, you can use the Stripes tag library with FreeMarker. To do this you'll need to add a single assign
statement at the top of your FreeMarker templates:
Code Block | ||||
---|---|---|---|---|
| ||||
[#assign s=JspTaglibs["http://stripes.sourceforge.net/stripes.tld"]] |
...
The following is a reworking of the Quick Start Guide's JSP into a FreeMarker template. The ActionBean itself is identical except for the replacement of "index.jsp" with "index.ftl".
Code Block | ||||
---|---|---|---|---|
| ||||
[#ftl] [#assign s=JspTaglibs["http://stripes.sourceforge.net/stripes.tld"]] <html> <head> <title>My First Stripe</title> <style type="text/css"> input.error { background-color: yellow; } </style> </head> <body> <h1>Stripes Calculator - FTL</h1> Hi, I'm the Stripes Calculator. I can only do addition. Maybe, some day, a nice programmer will come along and teach me how to do other things? [@s.form action="/examples/quickstart/Calculator.action"] [@s.errors/] <table> <tr> <td>Number 1:</td> <td>[@s.text name="numberOne"/]</td> </tr> <tr> <td>Number 2:</td> <td>[@s.text name="numberTwo"/]</td> </tr> <tr> <td colspan="2"> [@s.submit name="addition" value="Add"/] [@s.submit name="division" value="Divide"/] </td> </tr> <tr> <td>Result:</td> <td>${(actionBean.result)!}</td> </tr> </table> [/@] </body> </html> |
...
The last significant change is the following line:
Code Block | ||
---|---|---|
| ||
<td>${(actionBean.result)!}</td> |
...