Introduction to Using FreeMarker in Spring MVC
Last Updated :
24 Apr, 2025
FreeMarker is the Apache Software Foundation that produces the Java-based template engine. The MVC paradigm is supported by FreeMarker, just like it is by other template engines, for HTML web pages in apps. To utilize FreeMarker instead of JSP with Spring MVC, follow this guide to configure it. Four popular template engines are supported by Spring Boot out of the box. It has been the most reliable of these four in terms of performance. In this lesson, we will learn how to develop dynamic MVC views using this template engine. It is a view-layer technology. It's used to show data. Data that replaces JSP can be shown using FreeMarker.
Maven Dependencies
A JAR file that a Java program uses is all that constitutes a dependency in the Maven environment. Maven will download and add the JAR file to our Java path based on the information in the POM file.
Below are the Maven dependencies we need to add to the pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
Freemarker Template Files
Spring Boot searches for template files in the classpath:/templates/. And, as of Spring Boot 2.2, you should name your files with tthee.ftlh suffix. If you want to change the location or the file extension, use the following settings.
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.26-incubating</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
Configuration of FreeMarker in Spring MVC
Let's now explore the project settings. Since this is a Spring annotation-based project, we won't be going over the XML-based setup.
Step 1: Configuration of the Java class
First of you need to config the java class. Here is the java class.
Java
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureViewResolvers (ViewResolverRegistry registry) {
registry.freeMarker();
}
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/views/");
return configurer;
}
}
Step 2: Make Spring Web Configuration
In order to setup web components, you need to develop a class. To do this, we need to annotate the class with @ComponentScan, @EnableWebMvc, and @Configuration.
Java
@EnableWebMvc
@Configuration
@ComponentScan({"org.geekforgeeks.freemarker"})
public class SpringWebConfig extends WebMvcConfigurerAdapter {
}
Step 3: Put ViewResolver in configuration.
The Spring MVC Framework provides the ViewResolver interface, which connects view names to actual views. We'll create a FreeMarkerViewResolver object from the spring-webmvc requirement.
Java
@Bean
public FreeMarkerViewResolver freemarkerViewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setCache(true);
resolver.setPrefix("");
resolver.setSuffix("ftlh");
return resolver;
}
Step 4: Configuring the Path of the FreeMarker Template
Configure the template path, which specifies the location of the templates within the web context:
Java
@Bean
public FreeMarkerConfigurer freemarkerConfig() {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/ftth/");
return freeMarkerConfigurer;
}
Step 5: Setting Up a Spring Controller
A FreeMarker template may now be processed for display using a Spring Controller. This is only a standard Spring Controller:
Java
@RequestMapping(method = RequestMethod.GET)
@Controller
public class GreenPagesController {
@RequestMapping("/spring.html")
public void spring() {
}
}
Step 6: Create a Simple HTML Files
Make a new file called index.ftl in the folder /WebContent/WEB-INF/ftl/. Copy and paste the below text into it.
XML
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>FreeMarker Spring Boot Example</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Output:
.jpg)
Step 7: Import the spring boot starter FreeMarker dependency
Finally, we only need to import the spring-boot-starter-FreeMarker dependency if we're using Spring Boot:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>2.4.8.RELEASE</version>
</dependency>
Conclusion
So, this is introduction to using FreeMarker in Spring MVC. It has been the most reliable of these four in terms of performance. It is a View layer technology. It's used to show data. Data that replaces JSP can be shown using FreeMarker. FreeMarker is essentially a text file.
Similar Reads
Introduction to Spring Framework
The Spring Framework is a powerful, lightweight, and widely used Java framework for building enterprise applications. It provides a comprehensive programming and configuration model for Java-based applications, making development faster, scalable, and maintainable. Before Enterprise Java Beans (EJB)
9 min read
Introduction to Spring Security and its Features
Spring Security is a powerful authentication and authorization framework used to secure Java-based web applications. It easily integrates with Spring Boot and provides advanced security mechanisms such as OAuth2, JWT-based authentication, role-based access control, and protection against common vuln
3 min read
Introduction to Spring Boot
Spring is widely used for creating scalable applications. For web applications, Spring provides Spring MVC, a commonly used module for building robust web applications. The major drawback of traditional Spring projects is that configuration can be time-consuming and overwhelming for new developers.
5 min read
Spring MVC using Java Based Configuration
Spring MVC framework enables the separation of modules, namely Model, View, and Controller, and seamlessly handles application integration. This enables the developer to create complex applications using plain Java classes. The model object can be passed between the view and the controller using map
4 min read
10 Reasons to Use Spring Framework in Projects
Spring is the season that is known for fresh leaves, new flowers, and joy which makes our minds more creative. Do you know there is a bonus for us? We have another Spring as well. Our very own Spring framework! It is an open-source application framework that is used for building Java applications an
6 min read
How to Create Your First Model in Spring MVC?
Spring MVC is a powerful Web MVC framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components: Model: Represents the data of the application. It can be a single object or a collection of objects.Vie
6 min read
How to Create Your First View in Spring MVC?
Spring MVC is a powerful Web MVC Framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components: Model: Represents the data of the application. It can be a single object or a collection of objects.Vie
5 min read
Spring MVC - Create Registration Form using Form Tag Library
Spring Framework provides springâs form tag library for JSP views in Springâs Web MVC framework. In Spring Framework, we use Java Server Pages(JSP) as a view component to interact with the user. From version 2.0, Spring Framework provides a comprehensive set of data binding-aware tags. These tags ar
7 min read
Event Registration System using Spring Boot
Event Registration System plays an important role in the Event Management Business for tracking the Event related details and also we can adjust our time table also according to the events data. In this Article we will explain about the Event Registration System is works with a good example with rel
13 min read
How to Create a Project using Spring MVC and Hibernate 5?
Spring MVC is a popular model view controller framework that handles dependency injection at run time. Hibernate 5 is an ORM framework that acts as an abstraction over the database. It allows interaction with the underlying database by removing any implementation details which is handled by hibernat
4 min read