Home  >  Spring 4

Spring 4 MVC + Freemarker Template Annotation Integration Example with FreeMarkerConfigurer

By Arvind Rai, October 12, 2015
This page will walk through Spring 4 MVC and Freemarker template annotation integration example with FreeMarkerConfigurer. Spring provides FreeMarkerConfigurer that is used to set template loader path. It is internally used by FreeMarkerView. The values for Freemarker template is assigned in ModelAndView or Model and we set values using addObject() and addAttribute() in spring controller class. In our example, our template generates a HTML page for blogging.

Software Used in Demo

Find the software used in demo.
1. Java 8
2. Tomcat 8
3. Spring 4.1
4. Freemarker 2.3
5. Eclipse
6. Gradle

Project Structure in Eclipse

Find the project structure in eclipse.
Spring 4 MVC + Freemarker Template Annotation Integration Example with FreeMarkerConfigurer

Gradle to Resolve Dependency for Spring and Freemarker

Find the build.gradle file to resolve dependency for Spring and Freemarker.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'Spring4'
version = '1' 
repositories {
dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web:1.2.3.RELEASE'
    compile 'org.springframework:spring-context-support:4.1.6.RELEASE'
    compile 'org.freemarker:freemarker:2.3.23'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat:1.2.3.RELEASE'

Create Freemarker Template (FTL)

Find the Freemarker template which are being used in our demo. This will generate HTML file for a blog.
<head><title> ${blogTitle} </title>
<h1> ${blogTitle} </h1>
<#list references as reference>
    ${reference_index + 1}. <a href="${reference.url}"> ${reference.title} </a> <br/>

JavaConfig with FreeMarkerConfigurer and ViewResolverRegistry.freeMarker()

Find the JavaConfig used in our demo.
package com.concretepage.config;  
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import freemarker.template.utility.XmlEscape;
public class AppConfiguration extends WebMvcConfigurerAdapter  {
        @Bean(name ="freemarkerConfig")	
	public FreeMarkerConfigurer freemarkerConfig() {
    	  FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    	  Map<String, Object> map = new HashMap<>();
    	  map.put("xml_escape", new XmlEscape());
	  return configurer;
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        public void configureViewResolvers(ViewResolverRegistry registry) {

FreeMarkerConfigurer : We need to create a bean with name freemarkerConfig that will configure templateLoaderPath. FreeMarkerConfigurer is used internally by FreeMarkerView.
ViewResolverRegistry.freeMarker() : freeMarker() method configures view resolver with empty default view name prefix and default suffix as .ftl

Now find WebApplicationInitializer implementation class.
package com.concretepage.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class Initializer implements WebApplicationInitializer {
	public void onStartup(ServletContext servletContext) throws ServletException {  
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();  
        Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));  

Controller and Bean Class

Freemarker template variables get value from spring ModelAndView or Model and we set values using addObject() and addAttribute().
package com.concretepage.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.concretepage.bean.URL;
public class BlogController {
	private URL url;
	@RequestMapping(value = "/blog")
	 public ModelAndView getBlog(ModelAndView mv) {
	    mv.addObject("blogTitle", "Freemarker Template Demo using Spring");
	    mv.addObject("message", "Getting started with Freemarker.<br/>Find a Freemarker template demo using Spring.");
	    mv.addObject("references", url.getUrlList());
	    return mv;

package com.concretepage.bean;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
public class URL {
	private String url;
	private String title;
	public URL(){}
	public URL(String url, String title) {
		this.url = url;
		this.title = title;
	public String getUrl() {
		return url;
	public void setUrl(String url) {
		this.url = url;
	public String getTitle() {
		return title;
	public void setTitle(String title) {
		this.title = title;
	public List<URL> getUrlList() {
           List<URL> list = new ArrayList<>();
           list.add(new URL("http://url1.com","URL One"));
           list.add(new URL("http://url2.com","URL Two"));
           list.add(new URL("http://url3.com","URL Three"));
	   return list;


Run the URL http://localhost:8080/Spring4-1/blog and we will get output as follows.
Spring 4 MVC + Freemarker Template Annotation Integration Example with FreeMarkerConfigurer

Download Source Code

comments powered by Disqus

Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us