有没有办法让 Spring Thymeleaf 处理字符串模板? [英] Is there a way to make Spring Thymeleaf process a string template?

查看:34
本文介绍了有没有办法让 Spring Thymeleaf 处理字符串模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一些类似的东西:

I would like to write something like :

    @Autowired
    private SpringTemplateEngine engine;
....
  // Thymeleaf Context
  WebContext thymeleafContext = new WebContext(request, response, request.getServletContext(), locale);

    // cached html of a thymeleaf template file
    String cachedHtml=....

    // process the cached html
  String html=engine.process(cachedHtml, thymeleafContext);

默认情况下,[process] 方法不能这样做.我可以从文档中了解到我需要一个特殊的模板解析器:

By default, the [process] method can't do that. I can understand from the docs that I need a special Template Resolver :

为了执行模板,将使用 process(String, IContext) 方法:final String result = templateEngine.process("mytemplate", ctx);mytemplate"字符串参数是模板名称,它将以模板解析器/s 中配置的方式与模板本身的物理/逻辑位置相关.

有人知道如何解决我的问题吗?

Does anyone know how to solve my problem ?

目标是将 Thymeleaf 模板(文件)缓存在字符串中,然后处理这些字符串而不是文件.

The goal is to cache the Thymeleaf templates (files) in strings and then process theses strings rather than the files.

推荐答案

您可以实现自己的 TemplateResolverIResourceResolver 以使用 String.

You can implement your own TemplateResolver and IResourceResolver to work with String.

这篇关于有没有办法让 Spring Thymeleaf 处理字符串模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆