Skip to content

Thymeleaf ​

1.简介 ​

Thymeleaf 是用于在 Java Web 应用程序中渲染动态 HTML 的模板引擎,支持变量、循环和条件判断,可用于服务端网页及 HTML 邮件。Thymyleaf 是 tio-utils 提供的调用封装,名称保留其实际 Java API 拼写。

2. Tio-boot 整合 Thymeleaf 步骤 ​

在使用 tio-boot 框架时,通过整合 Thymeleaf 模板引擎,可以方便地渲染 HTML 页面。 tio-utils增加 Thymyleaf 和 ThymyleafEngine,用于支持 Thymeleaf.

tio-utils 通过自定义的 Thymyleaf 工具类,使开发者可以轻松地在控制器中调用 Thymeleaf 模板引擎进行 HTML 内容的渲染。Thymyleaf 工具类不仅支持多个 Thymeleaf 实例,还简化了模板的加载和渲染过程,使得 tio-boot 应用能够快速响应客户端请求并生成动态网页内容。

下面是具体的整合步骤:

  1. 添加 Thymeleaf 依赖: 在项目的 pom.xml 文件中添加 Thymeleaf 相关依赖,以便在项目中使用 Thymeleaf 进行模板渲染。

    xml
    <!-- Thymeleaf -->
    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf</artifactId>
    </dependency>
    
    <!-- 可选的 Java 时间类型扩展;HTML 模板由 Thymeleaf 核心处理 -->
    <dependency>
      <groupId>org.thymeleaf.extras</groupId>
      <artifactId>thymeleaf-extras-java8time</artifactId>
    </dependency>
  2. 创建 Thymeleaf 模板文件: 在 src/main/resources/templates/ 目录下创建 hello.html 文件,用于测试 Thymeleaf 模板的渲染。

    html
    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
      <head>
        <title>Hello Thymeleaf</title>
      </head>
      <body>
        <h1>Hello, <span th:text="${name}">Name</span>!</h1>
      </body>
    </html>
  3. 配置 Thymeleaf 模板引擎: 创建 ThymeleafConfig 类来配置 Thymeleaf 模板引擎,并注册到 Thymyleaf 实用工具类中,以便在整个应用中使用。

    java
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
    
    import nexus.io.annotation.AConfiguration;
    import nexus.io.annotation.Initialization;
    import nexus.io.tio.utils.thymeleaf.Thymyleaf;
    import nexus.io.tio.utils.thymeleaf.ThymyleafEngine;
    
    @AConfiguration
    public class ThymeleafConfig {
    
      @Initialization
      public void config() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML");
        templateResolver.setCharacterEncoding("UTF-8");
    
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
    
        Thymyleaf.add(new ThymyleafEngine(templateEngine));
      }
    }
  4. 创建控制器: 在控制器中使用 Thymyleaf 实用工具类来渲染 Thymeleaf 模板,并将渲染结果作为 HTTP 响应返回。

    java
    import org.thymeleaf.context.Context;
    
    import nexus.io.tio.boot.http.TioRequestContext;
    import nexus.io.tio.http.common.HttpResponse;
    import nexus.io.annotation.RequestPath;
    import nexus.io.tio.http.server.util.Resps;
    import nexus.io.tio.utils.thymeleaf.Thymyleaf;
    
    @RequestPath("/hello")
    public class HelloController {
    
      public HttpResponse index() {
        HttpResponse response = TioRequestContext.getResponse();
        // 创建 Thymeleaf 上下文并设置变量
        Context context = new Context();
        context.setVariable("name", "World");
    
        // 渲染模板
        String html = Thymyleaf.process("hello", context);
    
        // 返回渲染后的 HTML
        return Resps.html(response, html);
      }
    }
  5. 返回的数据: 上述代码运行后,将会返回渲染后的 HTML 内容。

    html
    <!DOCTYPE html>
    <html>
      <head>
        <title>Hello Thymeleaf</title>
      </head>
      <body>
        <h1>Hello, <span>World</span>!</h1>
      </body>
    </html>

3. 支持多个 Thymyleaf ​

为了支持多个 Thymyleaf 应用,在 tio-utils 中增加了 Thymyleaf 和 ThymyleafEngine 类。

  • Thymyleaf:提供了管理多个 Thymyleaf 模板引擎实例的能力,可以通过名称选择特定的引擎实例进行渲染。

  • ThymyleafEngine:封装了 Thymeleaf 模板引擎的基本操作,如模板渲染、模板缓存清理等功能。

MIT Licensed