# 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. ## Install Save in your project root: # Thymeleaf — Modern Server-Side Java Template Engine ## Quick Use ```html

Hello, World!

``` ```java // Spring Boot controller @GetMapping("/greet") public String greet(Model model) { model.addAttribute("name", "Developer"); return "greeting"; } ``` ## 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 - https://github.com/thymeleaf/thymeleaf - https://www.thymeleaf.org/documentation.html --- Source: https://tokrepo.com/en/workflows/asset-5bc26eff Author: AI Open Source