Spring-Boot Thymeleaf Hello World Example.
By: Maheshwar Ligade
Tags: Programming, Spring Boot, Thymeleaf, Software Engineering, Java11

In this article, we will demonstrate to you how to develop thymeleaf view with a spring-boot.

Below is the high-level technology we are using.

Spring Boot 2.4.1

Thymeleaf 3.0.11.RELEASE

Tomcat embed 9.0.14

JUnit 5

Gradle

Java 11

Visual studio code

Let us jump into the https://start.spring.io/ and generate the project with the below starters.

spring-boot-starter-web and spring-boot-starter-thymeleaf

build.gradle

plugins { id 'org.springframework.boot' version '2.4.1' id 'io.spring.dependency-management' version '1.0.10.RELEASE' id 'java' } group = 'com.techwasti' version = '0.0.1-SNAPSHOT' sourceCompatibility = '11' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' } test { useJUnitPlatform() }

Thymeleaf:

Thymeleaf is a Java XML/XHTML/HTML5 template engine that can work both in web and non-web environments. Thymeleaf is a java template engine that can work on both web and standalone mode. 😊

As we know spring-boot serves static content and we can keep that content in the resources folder.

Project Structure:

Let us create template/helloworld.html folder

<!DOCTYPE HTML> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>Spring Boot Thymeleaf Hello World Example</title> <link rel="stylesheet" th:href="@{webjars/bootstrap/4.2.1/css/bootstrap.min.css}"/> <link rel="stylesheet" th:href="@{/css/main.css}"/> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top"> <a class="navbar-brand" href="#">techwasti.com</a> </nav> <main role="main" class="container"> <div class="starter-template"> <h1>Spring Boot Thymeleaf Hello world</h1> <h2>

// placeholder to get message from Model <span th:text="'Hello, ' + ${message}"></span> </h2> </div> </main> <!-- /.container --> <script type="text/javascript" th:src="@{webjars/bootstrap/4.2.1/js/bootstrap.min.js}"></script> </body> </html>

Let us create HelloWorldController.java

package com.techwasti.sbhymleafhelloworld; import org.springframework.beans.factory.annotation.Value; 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 HelloWorldController { // this message we are reading from application.properties file @Value("${hello.message}") private String message;

@GetMapping("/") public String hellomessage(Model model) { model.addAttribute("message", message); return "helloworld"; //view name } // pass some parameter like /hello?name=thymeleaf @GetMapping("/hello") public String helloWithParam( @RequestParam(name = "name", required = true, defaultValue = "springboot") String name, Model model) { model.addAttribute("message", name); return "helloworld"; //view name } }

In the controller, we have a mapping for two methods one without param and one is with-param.

main.css:

Plain CSS having padding and some text color.

body { padding-top: 5rem; } .starter-template { padding: 3rem 1.5rem; text-align: center; } h1{ color:#51bdb4; } h2{ color:#582740; }

Now our code is ready, you can go ahead and start the spring application. Once an application is up hit the URL http://localhost:8080/ and you will be able to see the below output. This is without the param

If you pass the parameter then the output is: http://localhost:8080/hello?name=Java%20programming

If you don't want to pass or forgot to pass the value then it will take the default value which we have mentioned here,

@RequestParam(name = “name”, required = true, defaultValue = “springboot”)

Try out these things.

Source Code:-

https://github.com/maheshwarLigade/spring-boot-examples

$ git clone

$ cd sbhymleafhelloworld

$ gradle build

$ gradle bootRun $ git clone https://github.com/maheshwarLigade/spring-boot-examples $ cd sbhymleafhelloworld$ gradle build$ gradle bootRun

Conclusion:

Spring Boot is really the most popular framework in the java community to develop microservices, cloud, or web applications. Thymeleaf is java based template engine and it's a popular one to develop web applications.

More such articles:-

https://medium.com/techwasti

Let’s connect on Stackoverflow, LinkedIn, Facebook & Twitter.