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"/>

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

<bean id="freemarkerConfig" 
  <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>

<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"/>

Pom changes:

Remove the following:


Add the following:


Tag library changes:

Typical jsp will have the following:

<%@ taglib prefix="form" uri="" %>
<%@ taglib prefix="fmt"  uri="" %>
<%@ taglib prefix="c"    uri="" %>

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}" />


<fmt:message key=""/>

<@spring.message ''/>

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

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

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

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

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

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

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

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

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

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

<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"/>
<link rel="stylesheet" type="text/css" 
    href="<@spring.url '${css}/style.css'/>"/>

More information:Spring documentation

Actual source code examples: Petclincplus project in googlecode


  1. COOOOOL staffff! Thanx!

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

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

  3. Awesome Post, Neat and clean :)


Post a Comment

Popular posts from this blog

Opening a safe deposit locker in SBI

Opening a Kannada Word document

When gas leaks