Spring Boot Thymeleaf Hello World Example walks you through the process of creating a example spring boot with Thymeleaf 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 Thymeleaf 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-Thymeleaf-Example</artifactId> <name>SpringBoot-Thymeleaf-Example</name> <description>SpringBoot-Thymeleaf-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-thymeleaf</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 Thymeleaf 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-thymeleaf</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); } } |
But by default, Thymeleaf caches the templates, we need to change this behaviour in order to update Thymeleaf templates by setting application.properties file in src/main/resources directory like below:
1 |
spring.thymeleaf.cache: false |
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 Thymeleaf Example successfully"; model.addAttribute("name", name); model.addAttribute("message", message); return "hello"; } } |
Create the Thymeleaf template
Create index page using Thymeleaf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Spring Boot with Thymeleaf Example</title> </head> <body> <h2>Spring Boot Thymeleaf Example</h2> <p> <a href="hello"> Click me to say Hello</a> </p> </body> </html> |
Create a template using Thymeleaf to display “Hello World” words and other message.
1 2 3 4 5 6 7 8 9 10 11 |
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Hello World Thymeleaf!</title> </head> <body> <p th:text="${message}" /> <p th:text="'Hello, ' + ${name} + '!'" /> </body> </html> |
Run Application
To deploy and run our Spring Boot Thymeleaf 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 Thymeleaf Hello World Example.
Download complete source code, please click link below
SpringBoot-Thymeleaf-Example.zip (388 downloads)
Fascinating blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple tweeks would really make my blog stand out.
Please let me know where you got your theme. Thanks
Hi, i use mantle wordpress theme