Spring Boot with FreeMarker
FreeMarker is a powerful Java-based template engine used to generate dynamic web pages. This guide covers the key concepts and steps for using FreeMarker with Spring Boot, including setting up dependencies, creating controllers, designing FreeMarker templates, and rendering views.
Key Concepts of Spring Boot with FreeMarker
- Template Engine: FreeMarker is a template engine for generating dynamic web content.
- Template Resolver: FreeMarker uses template resolvers to find and process templates.
- Context: A data container used for passing variables to templates.
- Expressions: FreeMarker provides various expressions for variable substitution, text, and URL creation.
Setting Up Dependencies
Include the Spring Boot Starter FreeMarker dependency in your pom.xml
file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
Creating a Controller
Create a controller to handle web requests and return FreeMarker templates:
Example: HomeController.java
// 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 home(Model model) {
model.addAttribute("message", "Hello, FreeMarker with Spring Boot!");
return "index";
}
}
Designing FreeMarker Templates
Create FreeMarker templates to render HTML views. Place your templates in the src/main/resources/templates
directory:
Example: index.ftl
<!DOCTYPE html>
<html>
<head>
<title>Spring Boot with FreeMarker</title>
</head>
<body>
<h1>Welcome to Spring Boot with FreeMarker</h1>
<p>Message: ${message}</p>
</body>
</html>
Rendering Views
When the controller method returns a view name, Spring Boot will resolve the view using FreeMarker and render the template:
// 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 home(Model model) {
model.addAttribute("message", "Hello, FreeMarker with Spring Boot!");
return "index";
}
}
Using FreeMarker Expressions
FreeMarker provides various expressions for variable substitution, text, and URL creation:
- Variable Expressions:
${...}
- Directives:
<#...>...</#...>
- Built-ins: Functions for manipulating variables, such as
?upper_case
- Functions: Built-in and custom functions for complex operations
Example: Using FreeMarker Expressions
<!DOCTYPE html>
<html>
<head>
<title>FreeMarker Expressions</title>
</head>
<body>
<h1>FreeMarker Expressions</h1>
<p>Variable Expression: ${message}</p>
<p>Upper Case: ${message?upper_case}</p>
<p>Date: ${.now}</p>
</body>
</html>
Handling Form Submissions
FreeMarker provides support for handling form submissions:
Example: UserController.java
// UserController.java
package com.example.demo.controller;
import com.example.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class UserController {
@GetMapping("/user")
public String showForm(Model model) {
model.addAttribute("user", new User());
return "userForm";
}
@PostMapping("/user")
public String submitForm(User user, Model model) {
model.addAttribute("user", user);
return "userResult";
}
}
Example: userForm.ftl
<!DOCTYPE html>
<html>
<head>
<title>User Form</title>
</head>
<body>
<h1>User Form</h1>
<form action="/user" method="post">
<label>Name: <input type="text" name="name" /></label><br/>
<label>Email: <input type="email" name="email" /></label><br/>
<button type="submit">Submit</button>
</form>
</body>
</html>
Example: userResult.ftl
<!DOCTYPE html>
<html>
<head>
<title>User Result</title>
</head>
<body>
<h1>User Result</h1>
<p>Name: ${user.name}</p>
<p>Email: ${user.email}</p>
</body>
</html>
Key Points
- Template Engine: FreeMarker is a template engine for generating dynamic web content.
- Template Resolver: FreeMarker uses template resolvers to find and process templates.
- Context: A data container used for passing variables to templates.
- Expressions: FreeMarker provides various expressions for variable substitution, text, and URL creation.
- Include the Spring Boot Starter FreeMarker dependency in your
pom.xml
file. - Create controllers to handle web requests and return FreeMarker templates.
- Design FreeMarker templates to render HTML views and place them in the
src/main/resources/templates
directory. - Use FreeMarker expressions for variable substitution, text, and URL creation in templates.
- Handle form submissions using FreeMarker and Spring Boot.
Conclusion
Spring Boot and FreeMarker provide a powerful combination for building dynamic web applications. By understanding and using the capabilities of FreeMarker with Spring Boot, developers can create flexible and maintainable web applications with ease. Happy coding!