[Note]www.tutorialspoint.com/spring_boot… Thymeleaf is a Java-based library for creating Web applications. It provides good support for XHTML/HTML5 in Web applications. In this article, you will learn the details about Thymeleaf.

Thymeleaf template

Thymeleaf converts your files into well-formed XML files. It contains the following six types of templates:

  • XML
  • Valid XML
  • XHTML
  • Valid XHTML
  • HTML5
  • Legacy HTML 5

All templates except legacy HTML5 are available in valid XML files. Legacy HTML5 allows us to render HTML5 tags in Web pages, including open tags.

The Web application

You can use the Thymeleaf template to create web applications in Spring Boot using the following steps.

Create an @Controller class file to redirect the request URI to an HTML file with the following code:

package com.tutorialspoint.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WebController {
   @RequestMapping(value = "/index")
   public String index(a) {
      return "index"; }}Copy the code

In the example above, the request URI is /index, controlling redirection to the index.html file. Note that the index.html file should go under the Templates directory and all JS and CSS files should go under the static directory of the classpath path. In our example, we used a CSS file to change the text color.

You can create a CSS file named styles.css in a separate directory using the following code:

h4 {
   color: red;
}
Copy the code

The code for the index.html file is as follows:

<! DOCTYPEhtml>
<html>
   <head>
      <meta charset = "ISO-8859-1" />
      <link href = "css/styles.css" rel = "stylesheet"/>
      <title>Spring Boot Application</title>
   </head>
   <body>
      <h4>Welcome to Thymeleaf Spring Boot web application</h4>
   </body>
</html>
Copy the code

A screenshot of the project browser looks like this:Now we need to add the Spring Boot Starter Thymeleaf dependency to our build configuration file. Maven users can add the following dependencies to the POM.xml file:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Copy the code

Gradle users can add the following dependencies to the build. Gradle file:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
Copy the code

The main Spring Boot application class code is as follows:

package com.tutorialspoint.demo;

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

@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code

Maven-pom.xml code is as follows:


      
<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>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1 - the SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.8. RELEASE</version>
      <relativePath />
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <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-test</artifactId>
         <scope>test</scope>
      </dependency>

      <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>
Copy the code

Gradle — build. Gradle code looks like this:

buildscript {
   ext {
      springBootVersion = '1. 58..RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.tutorialspoint'
version = '0. 01.-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}
dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
   compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
   testCompile('org.springframework.boot:spring-boot-starter-test')
}
Copy the code

Now you can create the executable JAR file and run the Spring Boot application using Maven or Gradle commands:

The Maven command is as follows:

mvn clean install
Copy the code

After “BUILD SUCCESS,” you can find the JAR files in the target directory.

Gradle can use the following commands:

gradle clean build
Copy the code

After “BUILD SUCCESSFUL”, you can find the JAR files in the BUILD /libs directory.

Run the JAR file with the following command:

Java - jar < JARFILE >Copy the code

Now the application has been started on Tomcat port 8080, as shown below:Enter the following URL in your browser and you should see output like the following:http://localhost:8080/index