Thymeleaf

1.简介

Thymyleaf 是一个用于在 Java Web 应用程序中渲染动态 HTML 内容的模板引擎。它通过使用特定的模板标签语法,使得在 HTML 文件中可以动态插入变量、循环、条件判断等逻辑,从而生成最终的 HTML 页面。Thymyleaf 非常适合用来生成服务器端渲染的网页,也可以用于生成 HTML 邮件内容。

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 进行模板渲染。

    <!-- Thymeleaf -->
    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf</artifactId>
      <version>3.0.15.RELEASE</version>
    </dependency>
    
    <!-- Thymeleaf 模板引擎的 HTML5 支持 -->
    <dependency>
      <groupId>org.thymeleaf.extras</groupId>
      <artifactId>thymeleaf-extras-java8time</artifactId>
      <version>3.0.4.RELEASE</version>
    </dependency>
    
  2. 创建 Thymeleaf 模板文件: 在 src/main/resources/templates/ 目录下创建 hello.html 文件,用于测试 Thymeleaf 模板的渲染。

    <!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 实用工具类中,以便在整个应用中使用。

    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
    
    import com.litongjava.jfinal.aop.annotation.AConfiguration;
    import com.litongjava.jfinal.aop.annotation.AInitialization;
    import com.litongjava.tio.utils.thymeleaf.Thymyleaf;
    import com.litongjava.tio.utils.thymeleaf.ThymyleafEngine;
    
    @AConfiguration
    public class ThymeleafConfig {
    
      @AInitialization
      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 响应返回。

    import org.thymeleaf.context.Context;
    
    import com.litongjava.tio.boot.http.TioRequestContext;
    import com.litongjava.tio.http.common.HttpResponse;
    import com.litongjava.tio.http.server.annotation.RequestPath;
    import com.litongjava.tio.http.server.util.Resps;
    import com.litongjava.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 内容。

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

3. 支持多个 Thymyleaf

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

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

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