Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsJul 19, 2026·3 min de lecture

Thymeleaf — Modern Server-Side Java Template Engine

Thymeleaf is a Java template engine for web and standalone environments that processes HTML, XML, JavaScript, CSS, and plain text with natural templating that renders correctly in browsers.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Thymeleaf Overview
Commande d'installation directe
npx -y tokrepo@latest install 5bc26eff-83b2-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

Introduction

Thymeleaf is a server-side Java template engine designed for producing HTML in web applications. Its natural templating approach means templates are valid HTML files that designers can open in a browser without running a server. Thymeleaf integrates seamlessly with Spring Boot as its default view technology, replacing JSP in modern Java web development.

What Thymeleaf Does

  • Renders HTML templates on the server using data from Java controllers
  • Supports natural templating — templates display meaningful content when opened as static HTML
  • Processes expressions, conditionals, loops, and fragments using HTML attributes prefixed with th:
  • Integrates with Spring MVC, Spring WebFlux, and standalone Java applications
  • Supports template layouts, fragments, and component-style reuse

Architecture Overview

Thymeleaf parses templates into an internal DOM-like model, evaluates expressions against the context (a map of model attributes), and writes the processed output. The Standard Dialect provides th: attributes for text interpolation, iteration, conditional rendering, and URL rewriting. Dialects are extensible — Spring adds form binding, security, and message resolution attributes. Template resolvers locate templates by name from the classpath, file system, or custom sources. A template cache avoids re-parsing on every request.

Self-Hosting & Configuration

  • In Spring Boot, add spring-boot-starter-thymeleaf — auto-configuration handles the view resolver
  • Place HTML templates in src/main/resources/templates/
  • Use th:text, th:each, th:if, and th:href attributes to bind data to HTML elements
  • For standalone use, create a TemplateEngine with a ClassLoaderTemplateResolver
  • Configure caching, encoding, and template mode in application.properties

Key Features

  • Natural templates render as valid HTML in browsers and prototyping tools
  • Expression language (OGNL or SpringEL) provides rich data access and formatting
  • Fragment system enables reusable components, layouts, and decorator patterns
  • Spring Security dialect integrates authorization checks directly in templates
  • Internationalization support via message bundles and #{...} expressions

Comparison with Similar Tools

  • JSP — Legacy Java template technology requiring a servlet container; Thymeleaf works anywhere and produces cleaner HTML
  • FreeMarker — Powerful template engine with its own syntax; Thymeleaf uses HTML attributes for a more natural developer experience
  • Mustache — Logic-less templates; Thymeleaf provides richer expressions and Spring integration
  • React/Vue SSR — JavaScript-based rendering; Thymeleaf is JVM-native with no Node.js dependency
  • Pebble — Twig-inspired Java templates; Thymeleaf has deeper Spring ecosystem support

FAQ

Q: Is Thymeleaf the default template engine in Spring Boot? A: Yes. Adding spring-boot-starter-thymeleaf auto-configures Thymeleaf as the view resolver.

Q: Can Thymeleaf be used outside Spring? A: Yes. Thymeleaf has a standalone API. Create a TemplateEngine, configure a resolver, and call process() with a context map.

Q: How does natural templating work? A: Thymeleaf uses HTML attributes (th:text, th:each) that browsers ignore when opening the file statically. The static content acts as a placeholder until the server processes the template.

Q: Does Thymeleaf support reactive web applications? A: Yes. Thymeleaf integrates with Spring WebFlux and supports reactive data-driven rendering with chunked output.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires