Tutorial describing how to create the simple Hello World application using Spring Boot and Freemarker template engine.
Technologies used:
- Spring Boot 2.1.7.RELEASE
- JDK 1.8
- Maven 3
- Freemarker
Project setup
Project dependencies managed by the Maven’s pom.xml configuration file:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> </parent> <groupId>com.devcases.springboot</groupId> <artifactId>springboot-helloworld-freemarker</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <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>
The explanation of the above dependencies:
spring-boot-starter-web
– packages all the necessary dependencies together with the auto configuration for running simple web applicationspring-boot-starter-tomcat
– provides the embedded tomcat application serverspring-boot-starter-freemarker
– provides auto configuration for Freemarker templatesspring-boot-maven-plugin
– plugin prividing commands to work with a Spring Boot application (we will be usine thespring-boot:run
command)
Application Initializer
The standard spring boot application initializer, which our application need to provide:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Controller
The standard controller containing rest services:
@Controller
public class HelloWorldController {
private static final String message = "Hello World";
@GetMapping(value = "/")
public String hello(Map<String, Object> model) {
model.put("message", message);
return "hello";
}
}
The controller has only one endpoint available at the root address (/
– in our example it is resolved to the localhost:8080
) . The method passes the message
parameter to the hello
template.
View
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>${message}</title> </head> <body> <h2 class="hello-title">${message}</h2> </body> </html>
The view contains a simple html page, that shows passed message
parameter.
Configuration
The configuration in application.properties
file contains information how to resolve Freemarker files. In our case, the templates (with suffix .ftl
) are located under resources the resources/ftl
directory:
spring.freemarker.template-loader-path: classpath:/ftl spring.freemarker.suffix: .ftl
Running
The project can be started by the running the following command:
mvn clean spring-boot:run
The command deploys the apllication on the embedded tomcat server.
By visiting the localhost:8080
url, we should see the page containg the Hello World message.
The example code you can download from here