Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Spring MVC and FreeMarker

FreeMarker is a Java-based template engine for generating dynamic web pages. This guide covers the key concepts and steps for integrating FreeMarker with Spring MVC, including setting up FreeMarker, creating FreeMarker templates, and using FreeMarker features in your Spring MVC application.

Key Concepts of Spring MVC and FreeMarker

  • FreeMarker Template Engine: A template engine that processes HTML, XML, JavaScript, CSS, and text files.
  • FreeMarkerViewResolver: A Spring MVC ViewResolver implementation that uses FreeMarker templates.
  • FreeMarker Expressions: Special syntax used to process templates, such as variable expressions, loops, and conditionals.

Setting Up FreeMarker

Include the necessary dependencies for FreeMarker in your pom.xml file:

<dependencies>
    <!-- Spring Boot Starter FreeMarker -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>

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

Configuring FreeMarker

Configure FreeMarker in your Spring MVC application by defining a FreeMarkerConfigurer bean:

WebConfig.java

// WebConfig.java
package com.example.springmvc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

@Configuration
public class WebConfig {

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() {
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPath("/WEB-INF/views/");
        return configurer;
    }

    @Bean
    public FreeMarkerViewResolver freemarkerViewResolver() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setCache(true);
        resolver.setPrefix("");
        resolver.setSuffix(".ftl");
        return resolver;
    }
}

Creating FreeMarker Templates

Create FreeMarker templates in the src/main/webapp/WEB-INF/views directory:

home.ftl

<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
</head>
<body>
    <h1>Home</h1>
    <p>${message}</p>
</body>
</html>

Creating the Controller

Create a controller to handle requests and use FreeMarker templates:

HomeController.java

// HomeController.java
package com.example.springmvc.controllers;

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

@Controller
public class HomeController {

    @GetMapping("/home")
    public String home(Model model) {
        model.addAttribute("message", "Welcome to Spring MVC with FreeMarker!");
        return "home";
    }
}

Using FreeMarker Features

FreeMarker provides various features for template processing, such as variable expressions, loops, and conditionals:

Variable Expressions

<p>${message}</p>

Loops

<#list items as item>
    <p>${item}</p>
</#list>

Conditionals

<#if condition>
    <p>Condition is true</p>
</#if>

Key Points

  • FreeMarker Template Engine: A template engine that processes HTML, XML, JavaScript, CSS, and text files.
  • FreeMarkerViewResolver: A Spring MVC ViewResolver implementation that uses FreeMarker templates.
  • FreeMarker Expressions: Special syntax used to process templates, such as variable expressions, loops, and conditionals.
  • Include the necessary dependencies for FreeMarker in your pom.xml file.
  • Configure FreeMarker in your Spring MVC application by defining a FreeMarkerConfigurer bean.
  • Create FreeMarker templates in the src/main/webapp/WEB-INF/views directory.
  • Create a controller to handle requests and use FreeMarker templates.
  • Use FreeMarker features such as variable expressions, loops, and conditionals to process templates.

Conclusion

Integrating FreeMarker with Spring MVC allows you to create dynamic and interactive web applications. By setting up FreeMarker, creating FreeMarker templates, and using FreeMarker features, developers can enhance their Spring MVC applications and provide a rich user experience. Happy coding!