Explain Thymeleaf with example

Explain Thymeleaf with example

Thymeleaf is a modern server-side Java template engine for web and standalone environments. It is designed to process and generate HTML, XML, JavaScript, CSS, and text. Thymeleaf is commonly used as a view template engine in Spring applications to render web pages with dynamic content.

Key Features of Thymeleaf:

  1. Natural Templating: Thymeleaf templates are designed to be natural templates, meaning they can be rendered as static prototypes in a web browser without needing the server to process them.
  2. Expression Language: Thymeleaf uses a powerful expression language to access data and perform logic within the templates.
  3. Integration with Spring: Thymeleaf integrates seamlessly with Spring MVC, making it easy to use in Spring-based applications.
  4. Internationalization: Supports internationalization and localization of content.
  5. Flexible and Extensible: Provides a flexible and extensible architecture to handle various templating needs.

Thymeleaf with example

Example of Thymeleaf with example

    Add Dependency

    Include Thymeleaf dependency in your pom.xml for Maven or build.gradle for Gradle.

    1. Setting Up Thymeleaf in a Spring Boot Application

    1. Setting Up Thymeleaf in a Spring Boot Application
    
    Maven Dependency:
    ```xml
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    ```
    
    Gradle Dependency:
    ```groovy
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    ```
    

    2. Creating a Thymeleaf Template

    Example
    
    Create an HTML Template (`src/main/resources/templates/index.html`):
    ```html
    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Thymeleaf Example</title>
    </head>
    <body>
        <h1 th:text="'Hello, ' + ${name} + '!'"></h1>
    </body>
    </html>
    ```
    

    Explanation:

    • xmlns:th="http://www.thymeleaf.org": Declares Thymeleaf namespace.
    • th:text="'Hello, ' + ${name} + '!': Uses Thymeleaf expression to insert dynamic content.

    3. Creating a Controller

      Define a Spring Controller (src/main/java/com/example/demo/controller/HomeController.java):

      Example
      
      ```java
      package com.example.demo.controller;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.GetMapping;
      
      @Controller
      public class HomeController {
      
          @GetMapping("/greet")
          public String greet(Model model) {
              model.addAttribute("name", "World");
              return "index";
          }
      }
      ```
      

      Explanation:

      • @Controller: Marks the class as a Spring MVC controller.
      • @GetMapping("/greet"): Maps the /greet URL to the greet method.
      • model.addAttribute("name", "World"): Adds an attribute to the model, which Thymeleaf will use in the template.

      4. Running the Application

        When you run the Spring Boot application and navigate to /greet, Thymeleaf processes the index.html template and renders the page with the dynamic content.

        Output:

        • The HTML page will display: Hello, World!

        Conclusion of Thymeleaf with example

        • Thymeleaf: A server-side Java template engine used for rendering dynamic web content.
        • Features: Natural templating, powerful expression language, integration with Spring, internationalization, and flexibility.
        • Example: Demonstrated setting up Thymeleaf in a Spring Boot application, creating a template, and integrating it with a Spring controller.

        Homepage

        Readmore