Spring Boot FreeMarker Hello World Example

Spring Boot FreeMarker Hello World Example walks you through creating a example spring boot with FreeMarker template engine step by step. Spring Boot is sub-project developed by developers of spring framework, Spring Boot makes it easy to create stand-alone, production-grade Spring based applications with minimum configuration possible. This tutorial is quite familiar to the tutorial Spring Boot Maven Example Hello World with JSP.

Other interesting posts you may like

Let’s begin:

Project structure
We will create a project with directory structure like below

Spring Boot FreeMarker Hello World Example

Maven Dependencies
To create Spring Boot FreeMarker Hello World Example, you need to add dependencies into pom.xml file like below:

Let’s dig deeper:

We are developing a web application so we will add a spring-boot-starter-web dependency in pom.xml like below:
In order to enable FreeMarker support we need to add dependencies into pom.xml like below:

In this example, we are using maven plugin so we need to add the below dependency into pom.xml file:

Configure Spring Web Application
To set our application up as a servlet application we extend our main class with SpringBootServletInitializer and override the configure method using SpringApplicationBuilder.

Configuration properties
We are setting these below properties in application.properties for FreeMarker templates file, stored src/main/resources directory like below:

Or we can use WebMvcConfigurerAdapter instead of using the above properites like below:

Create the Controller
We create a simple controller named HelloWorldController like below.

Create the FreeMarker template

Create index page using FreeMarker

Create a template using FreeMarker to display “Hello World” words and other message.

Run Application
To deploy and run our Spring Boot FreeMarker Hello World Example on web container, we run the below command in console

Note: You must be in your project directory before run that command.
While running that command, you will see the output like below

Spring Boot FreeMarker Hello World Example

After running successfully, you can access the application via URL http://localhost:8080 and see the homepage such as

Spring Boot FreeMarker Hello World Example

Click the link “Click me to say Hello” then the Hello World page is displayed

Spring Boot FreeMarker Hello World Example

That’s it on the tutorial Spring Boot FreeMarker Hello World Example.

Download complete source code, please click link below

SpringBoot-Freemarker-Example.zip (98 downloads)

GitHub: https://github.com/javabycode/spring-boot-freemarker-hello-world-example

Leave a Comment

*

Please share it if you found this useful
Hide Buttons