FreeMarker

"FreeMarker is Java template; a generic tool to generate text output (HTML, RTF, source code, etc.)..."

Description

Integration with FreeMarker is done through FreeMarkerViewAdapter. It is a view implementation used by MVCConnectionHandler.

FreeMarkerViewAdapter obtains template from supplied view name (using suffix if supplied) and processes it using a model that is passed in.

Configuration

FreeMarkerViewAdapter has two important parameters: templatesLocation and suffix. The first is self-explanatory, while the second represents a string that is suffixed to the template name supplied by ModelAndView. Suffix can be set to null and in that case it is going to be treated as suffix is set to "".

FreeMarkerViewAdapter's init method must be called after templates location is set. Templates' location can be set as a file or URL.

Example

Here is an example of an configuration of FreeMarker (with Spring Webflow) taken from supplied demo:

<!--
 * This bean defines beans application context.
 * It references MVC controller defined at the "/guess.do" path.
-->
<bean name="webflow-freemarker-example-applicaiton" class="org.abstracthorizon.danube.http.HTTPContext">
  <property name="components">
    <list>
      <bean class="org.abstracthorizon.danube.http.matcher.Prefix">
        <property name="prefix"><value>/guess.do</value></property>
        <property name="connectionHandler"><ref bean="guess.do"/></property>
      </bean>
    </list>
  </property>
</bean>

<!--
 * This is MVC controller that has Spring WebFlow as a controller and FreeMarker as a view adapter.
-->
<bean name="guess.do" class="org.abstracthorizon.danube.http.HTTPMVCConnectionHandler">
  <property name="controller"><ref bean="webFlowController" /></property>
  <property name="view"><ref bean="freemarkerViewAdapter" /></property>
</bean>

<!--
 * This is WebFlow controller. Flow locator is defined through flow registry bean and
 * "bean-explorer-flow" is set as defualt flow id
-->
<bean name="webFlowController" class="org.abstracthorizon.danube.webflow.DanubeFlowController">
  <property name="flowLocator"><ref bean="flowRegistry" /></property>
  <property name="defaultFlowId"><value>guess-flow</value></property>
</bean>

<!--
 * Flow registry defines where flow definitions are to be read from.
-->
<bean id="flowRegistry" class="org.springframework.webflow.registry.XmlFlowRegistryFactoryBean">
  <property name="flowLocations" value="guess-flow.xml"/>
</bean>

<!--
  This is FreeMarker view adapter.
  It defines directory "pages" as location of templates and ".page" as templates' suffix.
-->
<bean id="freemarkerViewAdapter" class="org.abstracthorizon.danube.freemarker.FreeMarkerViewAdapter" init-method="init">
  <property name="templatesURL" value="classpath:pages"/>
  <property name="suffix" value="page"/>
</bean>