Converting from jsp to freemarker

Here is an attempt to document the steps required to migrate the user interface of a spring-mvc web application, which uses jsp to freemarker template.

Update the context file

Typically jsp views are rendered using the following configuration:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  <property name="prefix" value="/WEB-INF/jsp/"/>
  <property name="suffix" value=".jsp"/>
</bean>

To use freemarker, we would change this to the following:

<bean id="freemarkerConfig" 
  class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</pre>

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
  <property name="cache" value="true"/>
  <property name="prefix" value=""/>
  <property name="suffix" value=".ftl"/>
</bean>

Pom changes:

Remove the following:

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>

Add the following:

<dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.16</version>
    </dependency>

Tag library changes:

Typical jsp will have the following:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt"  uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c"    uri="http://java.sun.com/jsp/jstl/core" %>

Instead we should add the following:

<#import "/spring.ftl" as spring />

The leading "/" is required as without it, freemarker will look for this
file in the same folder as the ftl file which includes it.

Comparison of jsp and freemarker syntax:


jsp freemarker

<c:out value="${variable}" />

${variable}

<fmt:message key="messages.properties.entry"/>

<@spring.message 'messages.properties.entry'/>

<c:url value="/relative/url"/>

<@spring.url '/relative/url'/>

<c:if test="${items.recentUpdate}" >
   <span><fmt:message key="pls.browse.new"/></span>
</c:if>

<#if item.recentUpdate >
    <span><@spring.message "pls.browse.new"/></span>
</#if>

<c:forEach items="${dir}" var="item">
   <a href="<c:url value="/browse/${item.absoluteFileName}"/>">
</c:forEach>

<#list dir as item>
    a href="<@spring.url "/browse/${item.absoluteFileName}"/>">
</#list>

<c:if test="${empty files && empty dir}">

<#if (files?size > 0) || (dir?size > 0)>

<li>
    <label for="contentFolder">
        <fmt:message key="pls.configure.folder"/></label>
    <form:errors path="contentFolder" cssClass="errors"/>
</li>
<li><form:input path="contentFolder" size="30" maxlength="80"/>
</li>

<li>
    <@spring.bind "settings.contentFolder"/>
    <label for="contentFolder">
<@spring.message 'pls.configure.folder'/></label>
    <@spring.showErrors ' ', 'errors'/>
</li>
<li>
<@spring.formInput 'settings.contentFolder', 'size="30" maxlength="80"'/>
</li>

<form:form modelAttribute="profile" method="POST">
    <form:errors cssClass="errors"/>


<@spring.bind "profile"/>
<@spring.showErrors ' ', 'errors'/>

<spring:theme var="css" code="css"/>
<link rel="stylesheet" type="text/css" 
href="<c:url value="${css}/style.css"/>"/>

<#assign css>
    <@spring.theme "css"/>
</#assign>
<link rel="stylesheet" type="text/css" 
    href="<@spring.url '${css}/style.css'/>"/>

More information:Spring documentation

Actual source code examples: Petclincplus project in googlecode

Comments

  1. COOOOOL staffff! Thanx!

    ReplyDelete
  2. Hi for me the code breaks whenever I apply <@spring.message> tags. I have imported spring.ftl then why it is breaking ?

    ReplyDelete
    Replies
    1. <#import "spring.ftl" as spring />
      ...
      ...
      <@spring.message "label.label" />
      ...
      ...

      Delete
  3. Awesome Post, Neat and clean :)

    ReplyDelete

Post a Comment

Popular posts from this blog

Opening a safe deposit locker in SBI

Opening a Kannada Word document

Switching to Tatasky make my pack