本文介绍了使用 thymeleaf 3 处理字符串模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用 StringTemplateResolver 来填充带有 Icontext 的字符串模板吗?如果是这样我们该怎么办?从 Thymeleaf 3 中删除了 TemplateProcessingParameters 和 IResourceResolver.任何工作示例都会有很大帮助?

Can we use StringTemplateResolver to populate a string template with Icontext. If so how we can do?TemplateProcessingParameters and IResourceResolver is removed from Thymeleaf 3. Any working example would greatly help?

我遵循了这个例子,它在 Thymeleaf 2 中效果很好
有没有办法制作 Spring Thymeleaf处理一个字符串模板?

I have followed this example and it works great in Thymeleaf 2
Is there a way to make Spring Thymeleaf process a string template?

我也没有看到任何参考任何迁移指南.

I didnt see any reference any migration guide as well.

推荐答案

我想我找到了解决方案.如果有人有更好的答案,请告诉我.我之前犯了一个小错误.希望这会有所帮助.

I think I found a solution. If anybody has better answer please let me know.I did a small mistake earlier. Hope this helps.

private TemplateEngine templateEngine;

private TemplateEngine getTemplateEngine() {
        if(null == templateEngine){
            templateEngine = new TemplateEngine();
            StringTemplateResolver templateResolver =new   StringTemplateResolver();
            templateResolver.setTemplateMode(TemplateMode.HTML);
            templateEngine.setTemplateResolver(templateResolver);
        }
        return templateEngine;
    }




public String getTemplateFromMap(String htmlContent, Map<String, String> dynamicAttibutesMap) {
    templateEngine = getTemplateEngine();
    String template = null;
    final Context ctx = new Context(new Locale(TEMPLATE_LOCAL));
    if (!CollectionUtils.isEmpty(emailAttibutesMap)) {
        dynamicAttibutesMap.forEach((k,v)->ctx.setVariable(k, v));
    }
    if (null != templateEngine) {
        template = templateEngine.process(htmlContent, ctx);
    } 
    return template;
}

这篇关于使用 thymeleaf 3 处理字符串模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 12:15