Spring Boot Thymeleaf Example Tutorial

In this tutorial, we will learn how to create a simple Spring boot hello world example with Thymeleaf .
Spring Boot aims to make it easy to create Spring-powered, production-grade applications and services with minimum fuss. It takes an opinionated view of the Spring platform so that new and existing users can quickly get to the bits they need.
Thymeleaf is a Java-based library used to create a web application. It provides a good support for serving a XHTML/HTML5 in web applications.
Learn spring boot at https://www.javaguides.net/p/spring-boot-tutorial.html.

Tools and technologies used

Development Steps

1. Create a Spring Boot Application

There are many ways to create a Spring Boot application. You can refer below articles to create a Spring Boot application.

2. Add maven dependencies

<?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.2.6.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>
    <groupId>net.javaguides</groupId>
    <artifactId>springboot-thymeleaf-hello-world-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-thymeleaf-hello-world-example</name>
    <description>Demo project for Spring Boot and thymeleaf</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3. Create Spring MVC Controller

Let's create a simple spring MVC controller with method handler:
package net.javaguides.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {

    @GetMapping({
        "/",
        "/hello"
    })
    public String hello(@RequestParam(value = "name",
        defaultValue = "World", required = true) String name, Model model) {
        model.addAttribute("name", name);
        return "hello";
    }
}

4. Thymeleaf ViewResolver Configuration

Spring boot will auto configure view resolver for Thymeleaf whenever it will find the springboot-thymeleaf-starter dependency on classpath. Spring boot will pick thymeleaf templates (HTML pages) from resources/templates folder.

5. Create Thymeleaf Page

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

6. Run app and demo

Let's run this spring boot application from IDE -> Right click -> Run As -> Java Application:
package net.javaguides.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootThymeleafHelloWorldExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootThymeleafHelloWorldExampleApplication.class, args);
    }
}

Demo

Demo of this tutorial covered in below video tutorial:

Conclusion

In this tutorial, we have learned how to create a simple Spring boot hello world example with Thymeleaf.
Learn spring boot at https://www.javaguides.net/p/spring-boot-tutorial.html.

Comments