Spring Boot, the popular Java-based framework for building microservices and production-ready applications, offers excellent support for various templating engines. Freemarker, a comprehensive template engine, is one of them. This tutorial guides you through integrating Freemarker with Spring Boot to leverage its powerful features and create dynamic web pages.

Before diving in, make sure you have Java Development Kit (JDK) 1.8 or later, Apache Maven, and a favorite code editor or IDE installed. This tutorial assumes you're using Spring Boot 2.5.0 and Freemarker 2.3.30.

Setting Up the Project
First, let's configure Freemarker in a Spring Boot project. If you're using Spring Initializr, add 'Freemarker' under 'Dependencies'. For an existing project, add the following dependency to your pom.xml:

```xml
Next, configure Freemarker settings in application.properties:
```properties spring.freemarker.cache=false spring.freemarker.suffix=.ftl spring.freemarker.content-type=text/html spring.freemarker.template-loader-path=classpath:/templates/ ```
Creating a Simple template

Create a new file named hello.ftl in src/main/resources/templates/ directory. Add the following content:
```html
Hello, World!
Have a nice day!

```
Accessing the Template
Create a new Spring MVC controller to access the template:
```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class MainController { @GetMapping("/") public String viewHomePage() { return "hello"; } } ```
Now, accessing http://localhost:8080/ should display the "Hello, World!" message on your browser.

Dynamic Content
Let's make the template dynamic by adding some content from the Spring Boot application.









Create a new model class for data:
```java public class Greeting { private String name; private String message; public Greeting(String name, String message) { this.name = name; this.message = message; } } ```
Passing Data to the Template
Update the controller to pass data to the template:
```java import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class MainController { @GetMapping("/") public String viewHomePage(Model model) { Greeting greeting = new Greeting("World", "Have a nice day!"); model.addAttribute("greeting", greeting); return "hello"; } } ```
Update the Freemarker template hello.ftl to access the data:
```html
Hello, ${greeting.name}!
${greeting.message}
```
Accessing http://localhost:8080/ now displays "Hello, World!" followed by "Have a nice day!" thanks to the dynamic content.
Else Clause andstill/otherwise Directives
Freemarker provides directives like else, still, and otherwise to create conditional logic. Here's an example using still:
```html
${greeting.message still 'Something went wrong!'}
```
If greeting.message is null, it will display "Something went wrong!"
With these steps, you've successfully integrated Freemarker into your Spring Boot project, created dynamic content, and explored some Freemarker directives. Dive deeper into Freemarker documentation to unlock its full potential in your Spring Boot applications. Happy coding!