2.4.11.2. Screen Development Using FreeMarker

This section explains screen development using FreeMarker(external site) .

The installation procedure of FreeMarker is as follows. For more information on the procedure, see FreeMarker document .

  1. Add FreeMarker to the dependant library
  2. Configure the FreeMarkerServlet settings
  3. Create a template file (ftl file) to implement an action
  4. Prevent duplicate form submission (only if necessary)

2.4.11.2.1. Add FreeMarker to the dependant library

Add FreeMarker to dependent libraries to make it available to projects. When using Maven, add the following to POM. This manual has been confirmed with the version described in dependency below.

<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker-gae</artifactId>
  <version>2.3.27-incubating</version>
</dependency>

2.4.11.2.2. Configure the FreeMarkerServlet settings

Register FreeMarkerServlet in web.xml and make it respond to *.ftl.

Example of web.xml
<servlet>
  <servlet-name>freemarker</servlet-name>
  <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>freemarker</servlet-name>
  <url-pattern>*.ftl</url-pattern>

  <init-param>
    <!--
    Specify the placement directory of the template file (ftl).
    If "/" is specified, the root directory of war is used as the deployment directory.
    -->
    <param-name>TemplatePath</param-name>
    <param-value>/</param-value>

    <!-- For other than the above, refer to the document and configure the required parameters -->
  </init-param>
</servlet-mapping>

2.4.11.2.3. Create a template file (ftl file) to implement an action

Create and place a template file (ftl file). (For how to create a template, refer to the FreeMarker documentation) Action class returns the path to the template file as a response.

For example, to return HTML to the client using webapp/WEB-INF/template/index.ftl, action class returns the response as shown below. (webapp is the root directory of war)

return new HttpResponse("/WEB-INF/template/index.ftl");

Tip

The mechanism to return the HTML generated by FreeMarker to the client is as follows.

  1. HTTP Response Handler performs Servlet forward to /WEB-INF/template/index.ftl.
  2. In response to the ftl extension, FreeMarkerServlet is executed to generate HTML based on the template, request scope, and other data.
  3. Return the HTML that is generated to the client.

2.4.11.2.4. Prevent double submission

To prevent double submission, create an action by referring to UseToken Interceptor.