Mastering FTL (Freemarker Template Language) with Spring Boot: A Complete Guide for Java Developers

Mamta Yadav
4 min readMar 4, 2025

--

In the ever-evolving world of web development, having the ability to dynamically render content is crucial. When working with Java-based web applications, Freemarker (often referred to as FTL for Freemarker Template Language) is a powerful tool to handle dynamic content rendering. Paired with Spring Boot, FTL allows developers to separate their logic and presentation layers, making their applications more maintainable and scalable.

In this blog post, we will guide you through everything you need to know to integrate FTL (Freemarker Template Language) with Spring Boot. From dependencies to advanced features, let’s dive into how FTL works with Spring Boot and how you can use it to create dynamic and reusable templates.

What is FTL (Freemarker Template Language)?

FTL stands for Freemarker Template Language, which is the language used to write templates in Freemarker, a Java-based templating engine. FTL allows you to generate dynamic text content (HTML, XML, JSON, etc.) based on data passed from a Java backend. It’s widely used to build views in web applications, offering powerful features like loops, conditions, macros, and more.

In the context of Spring Boot, FTL templates act as the View in the Model-View-Controller (MVC) architecture. The controller sends data to the FTL templates, which then dynamically render the final output (such as an HTML page) to the user.

Why Use FTL with Spring Boot?

Using FTL in a Spring Boot application has several benefits:

  • Separation of Concerns: FTL allows you to separate the presentation logic (views) from the business logic (controllers).
  • Dynamic Rendering: Easily insert dynamic data into templates and control the output based on conditions.
  • Reusability: Define common components (headers, footers, navigation) as macros or include files for reuse across templates.
  • Powerful Features: Use loops, conditionals, and custom macros to render complex content in a clean and maintainable way.

How to Set Up FTL with Spring Boot?

Now let’s walk through the steps to integrate FTL with Spring Boot. We will cover the dependencies, configuration, and the creation of templates.

1. Add FTL Dependencies

To begin, you need to add Freemarker to your Spring Boot project. If you’re using Maven, add the following dependency in your pom.xml:

<dependencies>
<!-- Spring Boot Starter for Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Freemarker dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>

For Gradle users, add the following to your build.gradle:

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
}

2. Configure FTL in application.properties

Next, we’ll configure Spring Boot to use FTL. Add the following configuration to your src/main/resources/application.properties:

# Location for your templates
spring.freemarker.template-loader-path=classpath:/templates/

# File extension for the templates
spring.freemarker.suffix=.ftl

# Disable template caching for development purposes (set to true for production)
spring.freemarker.cache=false

This setup tells Spring Boot to look for templates in the /templates/ directory and use the .ftl extension for FTL templates.

3. Create FTL Templates

Once the dependencies and configurations are set, you can start creating FTL templates. Create the directory src/main/resources/templates/, and then add your FTL templates there. For example, create a file index.ftl:

<!DOCTYPE html>
<html>
<head>
<title>FTL Example</title>
</head>
<body>
<h1>Hello, ${user}!</h1>
</body>
</html>

Here, ${user} is a dynamic placeholder that will be replaced with the value passed from the Spring Boot controller.

4. Create a Spring Boot Controller

Now, let’s create a Spring Boot controller to send data to your FTL template. The controller will add attributes to the model and return the name of the template.

For example, here’s a simple HomeController.java:

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

@GetMapping("/")
public String index(Model model) {
// Add a dynamic variable to the model
model.addAttribute("user", "John Doe");
return "index"; // Return the name of the FTL template
}
}

In this controller, when the user accesses the root URL (/), the index method will add a user attribute to the model and return the index.ftl template. The placeholder ${user} in the template will be replaced with "John Doe".

5. Running the Application

Now that everything is set up, you can run your Spring Boot application. When you navigate to http://localhost:8080, you should see the output of the FTL template, with "John Doe" rendered where ${user} is placed.

Advanced FTL Features

Freemarker (FTL) has a rich set of features that you can use to create more dynamic and flexible templates. Here are some key features:

Loops in FTL

You can loop through collections using the <#list> directive in FTL. For example:

<ul>
<#list users as user>
<li>${user}</li>
</#list>
</ul>

This will loop through a list of users and create an unordered list.

Conditional Statements

You can use the <#if> directive for conditional rendering. For example:

<#if user == "John">
<p>Welcome, John!</p>
<#else>
<p>Welcome, guest!</p>
</#if>

Macros

Macros are reusable blocks of code that can be included in multiple templates. For example, create a header.ftl file with a reusable header component:

<#macro header>
<h1>Site Header</h1>
</#macro>

You can then include the macro in your main templates using:

<#include "header.ftl"/>

Best Practices for Using FTL with Spring Boot

Here are some best practices to make the most of FTL in Spring Boot:

  • Separation of Concerns: Keep your templates focused on presentation and your controllers focused on logic. Avoid mixing both in a single file.
  • Template Caching: Enable template caching in production to improve performance.
  • Use Macros and Includes: Reuse common components (like headers and footers) using macros or includes.
  • Test Templates: Write tests for controllers to ensure templates render as expected.
  • Handle Errors Gracefully: Always handle errors in controllers to prevent template failures.

Conclusion

Integrating FTL (Freemarker Template Language) with Spring Boot is a great way to build dynamic, maintainable, and reusable web applications. By separating the logic of your application from the presentation layer, you can create clean and scalable code. Whether you’re building small web pages or complex dynamic applications, FTL offers powerful features that allow you to render dynamic content with ease.

--

--

Mamta Yadav
Mamta Yadav

Written by Mamta Yadav

Information geek, TecH enthusiasm. ||||| Storyteller from my preliterate days. I write them down✍️

No responses yet