Configs2026年7月19日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Thymeleaf Overview
直接安装命令
npx -y tokrepo@latest install 5bc26eff-83b2-11f1-9bc6-00163e2b0d79 --target codex

先 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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产