Java.Thymeleaf.What is thymeleaf ?

📜 What is Thymeleaf?

Thymeleaf is a modern server-side Java template engine
that is used to generate HTML, XML, JavaScript, CSS, or even plain text dynamically.

✅ It is mainly designed for web applications, but can also be used for email templates or any kind of dynamic content generation.

Thymeleaf is often used as a replacement for JSP (JavaServer Pages) in modern Spring Boot and Spring MVC applications.

🔵 Key Characteristics of Thymeleaf

FeatureDescription
Template EngineProcesses HTML/XML templates on the server
Natural TemplatesTemplates can be opened directly in a browser even without a server (they look like normal HTML!)
Expression LanguageProvides ${...}, th:* attributes to insert dynamic data
Spring Boot IntegrationVery tightly integrated with Spring Boot (auto-configured)
Flexible OutputsCan generate HTML5, XML, JavaScript, Text, etc.
Reusable FragmentsSupports templates and fragments (like header, footer reuse)
Conditionals and LoopsEasily write if/else, for each directly in HTML

🔥 Small Example of Thymeleaf in a HTML Page

Suppose you have a Java controller that sets a model attribute:

model.addAttribute("username", "Stanley");

In your Thymeleaf template (welcome.html), you can use:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Welcome</title>
</head>
<body>

<h1 th:text="'Hello, ' + ${username} + '!'">Hello, Guest!</h1>

</body>
</html>

✅ The line:

<h1 th:text="'Hello, ' + ${username} + '!'">Hello, Guest!</h1>

Will replace the default content “Hello, Guest!” with “Hello, Stanley!” at runtime.

🔵 Some Important Thymeleaf Attributes

AttributePurpose
th:textSet the inner text of an element
th:hrefSet the href attribute of a link
th:srcSet the src attribute of an image
th:if / th:unlessConditional display of elements
th:eachLooping over collections (like forEach)
th:fragmentDefine a reusable piece of HTML
th:replaceReplace part of a page with a fragment

🛠️ Example of a Loop with Thymeleaf

Suppose you have a list of products:

model.addAttribute("products", List.of("Apple", "Banana", "Cherry"));

In Thymeleaf:

<ul>
    <li th:each="product : ${products}" th:text="${product}">Product</li>
</ul>

✅ Will generate:

<ul>
  <li>Apple</li>
  <li>Banana</li>
  <li>Cherry</li>
</ul>

🎯 Why is Thymeleaf Popular?

ReasonExplanation
Natural TemplatesYou can open them directly as static HTML when designing.
Spring SupportFull integration with Spring MVC and Spring Boot.
Modern SyntaxClean, HTML5-compliant, powerful expressions.
Reusable ComponentsLayouts, fragments, template inheritance.
Safe EvaluationGraceful handling of nulls, missing values, etc.
Easy learning curveEspecially for those who already know HTML and basic Java.

📢 In short:

Thymeleaf is like JSP for modern Spring Boot applications — but smarter, cleaner, and much easier to work with.

It uses pure HTML files with special attributes (th:*) to dynamically insert data.

🚀 Quick Real-World Usage

  • Spring Boot auto-configures Thymeleaf.
  • Place your .html files inside /resources/templates/.
  • Thymeleaf renders pages dynamically with model data.
  • You can build full web apps without touching JSP!
This entry was posted in Без рубрики. Bookmark the permalink.