Open In App

Introduction to Using FreeMarker in Spring MVC

Last Updated : 05 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Output in Browser

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads