spring - 使用 thymeleaf 3 处理字符串模板

标签 spring thymeleaf

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

我遵循了这个例子,它在 Thymeleaf 2 中很好用
Is there a way to make Spring Thymeleaf process a string template?

我也没有看到任何引用任何迁移指南。

最佳答案

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

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;
}

关于spring - 使用 thymeleaf 3 处理字符串模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39062780/

相关文章:

java - BeanFactory 未初始化或已关闭 - 在通过 ApplicationContext 访问 bean 之前调用 'refresh'

java - Mockito - 注入(inject)模拟列表

java - Thymeleaf 在已成功连接的 URL 上抛出 404?这种绑定(bind)正确吗?

java - Spring Boot + Thymeleaf 的@WebAppConfiguration 和@ContextConfiguration

Thymeleaf 嵌套列表/ArrayList - 获取所有元素的总大小

java - 无法在spring mvc中的 Controller 中接收表单数据

spring - 使用 Spring Boot 配置集成测试还是在单元测试中使用 Spring JDBC?

forms - struts2 Spring 表单提交错误

java - Spring Boot中自动将HashSet转换为串联字符串

spring - thymeleaf 和 spring-boot 以形式丢失的复杂对象