有没有办法使Spring Thymeleaf处理一个字符串模板?


问题内容

我想写一些像:

    @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]方法无法执行此操作。我从文档中了解到,我需要一个特殊的模板解析器:

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

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

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


问题答案:

您可以自己实现TemplateResolverIResourceResolver与一起使用String