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
Maven Dependencies
To create Spring Boot FreeMarker Hello World Example, you need to add dependencies into pom.xml file like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>SpringBoot-FreeMarker-Example</artifactId> <name>SpringBoot-FreeMarker-Example</name> <description>SpringBoot-FreeMarker-Example</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> </parent> <properties> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
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:
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> |
In this example, we are using maven plugin so we need to add the below dependency into pom.xml file:
1 2 3 4 |
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.javabycode.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; @SpringBootApplication public class MyWebApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(MyWebApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(MyWebApplication.class, args); } } |
Configuration properties
We are setting these below properties in application.properties for FreeMarker templates file, stored src/main/resources directory like below:
1 2 |
spring.freemarker.template-loader-path: / spring.freemarker.suffix: .ftl |
Or we can use WebMvcConfigurerAdapter instead of using the above properites like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.io.IOException; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; import freemarker.template.TemplateException; @Configuration public class MyWebConfig extends WebMvcConfigurerAdapter{ @Bean public ViewResolver viewResolver() { FreeMarkerViewResolver resolver = new FreeMarkerViewResolver(); resolver.setCache(true); resolver.setPrefix(""); resolver.setSuffix(".ftl"); resolver.setContentType("text/html; charset=UTF-8"); return resolver; } @Bean public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException { FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory(); factory.setTemplateLoaderPaths("classpath:templates", "src/main/resources/templates"); factory.setDefaultEncoding("UTF-8"); FreeMarkerConfigurer result = new FreeMarkerConfigurer(); result.setConfiguration(factory.createConfiguration()); return result; } } |
Create the Controller
We create a simple controller named HelloWorldController like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.javabycode.springboot; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloWorldController { @RequestMapping(value = "/", method = RequestMethod.GET) public String index() { return "index"; } @RequestMapping("/hello") public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) { String message="You just create Spring Boot FreeMarker Example successfully"; model.addAttribute("name", name); model.addAttribute("message", message); return "hello"; } } |
Create the FreeMarker template
Create index page using FreeMarker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Spring Boot FreeMarker Example</title> </head> <body> <h2>Spring Boot FreeMarker Example</h2> <p> <a href="hello"> Click me to say Hello</a> </p> </body> </html> |
Create a template using FreeMarker to display “Hello World” words and other message.
1 2 3 4 5 6 7 8 9 10 11 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello ${name}!</title> </head> <body> <p>${message}</> <h2>Hello ${name}!</h2> </body> </html> |
Run Application
To deploy and run our Spring Boot FreeMarker Hello World Example on web container, we run the below command in console
1 |
mvn spring-boot:run |
Note: You must be in your project directory before run that command.
While running that command, you will see the output like below
After running successfully, you can access the application via URL http://localhost:8080 and see the homepage such as
Click the link “Click me to say Hello” then the Hello World page is displayed
That’s it on the tutorial Spring Boot FreeMarker Hello World Example.
Download complete source code, please click link below
SpringBoot-Freemarker-Example.zip (397 downloads)
GitHub: https://github.com/javabycode/spring-boot-freemarker-hello-world-example