当前位置: 首页 > 知识库问答 >
问题:

Thymeleaf电子邮件模板和ConversionService

谭桐
2023-03-14

我有一个spring mvc应用程序,我试图将日期LocalDate呈现为字符串,对于普通视图,它可以工作,但对于电子邮件,它不工作,并抛出以下错误:

原因:org.springframework.core.convert.converterNotFoundException:找不到能够从[java.time.localdate]类型转换为[java.lang.string]类型的转换器

代码:

import org.thymeleaf.context.Context;
import org.thymeleaf.spring4.SpringTemplateEngine;

@Service
public class EmailService {

    @Autowired
    private SpringTemplateEngine templateEngine;

    public String prepareTemplate() {
        // ...
        Context context = new Context();
        this.templateEngine.process(template, context);
    }
}

共有1个答案

张智
2023-03-14

我进行了调试,发现如果我们使用新构造的上下文,它将创建另一个ConversionService实例,而不是使用DefaultFormattingConversionService bean。

在thymeleaf Spring的SpelVariableExpressionEvaulator中,我们可以看到以下代码

        final Map<String,Object> contextVariables =
                computeExpressionObjects(configuration, processingContext);

        EvaluationContext baseEvaluationContext =
                (EvaluationContext) processingContext.getContext().getVariables().
                        get(ThymeleafEvaluationContext.THYMELEAF_EVALUATION_CONTEXT_CONTEXT_VARIABLE_NAME);

        if (baseEvaluationContext == null) {
            // Using a standard one as base: we are losing bean resolution and conversion service!!
            baseEvaluationContext = new StandardEvaluationContext();
        }

为了解决这个问题,我们必须确保上下文包含使用正确的转换服务初始化的thymeleaf计算上下文。

import org.thymeleaf.context.Context;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.springframework.core.convert.ConversionService;
import org.springframework.context.ApplicationContext;

@Service
public class EmailService {

    @Autowired
    private SpringTemplateEngine templateEngine;

    // Inject this
    @Autowired
    private ApplicationContext applicationContext;

    // Inject this
    @Autowired
    private ConversionService mvcConversionService;

    public String prepareTemplate() {
        // ...
        Context context = new Context();
        // Add the below two lines
        final ThymeleafEvaluationContext evaluationContext = new ThymeleafEvaluationContext(applicationContext, mvcConversionService);
        context.setVariable(ThymeleafEvaluationContext.THYMELEAF_EVALUATION_CONTEXT_CONTEXT_VARIABLE_NAME, evaluationContext);
        this.templateEngine.process(template, context);
    }
}

问题解决了。

 类似资料:
  • 我正在开发一个应用程序,它在iframe中使用DocuSign签名,该应用程序位于一个专有的web应用程序中。签名者可以在同一个web应用程序中访问嵌入的iframe签名视图。 编辑:已解决 正如在答案的注释中提到的,通过根据收件人的值正确设置每个模板角色的ClientUserId,这个问题得到了解决。这允许使用模板角色签名者进行嵌入签名。

  • 发送电子邮件时,页脚中有下面的标准消息。 此电子邮件是使用CakePHP框架发送的,http://cakephp.org. 它似乎使用了这个: 我的控制器里有这个。 创建了以下视图: /应用程序/查看/电子邮件/文本/梦想。ctp /应用程序/视图/布局/电子邮件/文本/梦想。ctp 是否有任何其他设置我错过了cakephp使用我的布局? *注:如果我重新命名我的梦想。ctp默认。ctp它使用那个

  • 我遵循了这篇Thymeleaf教程“使用Thymeleaf在Spring生成丰富的HTML电子邮件”,使用Thymeleaf模板生成了一封电子邮件。一切正常,但我无法访问应用程序中其他地方使用的ApplicationContext bean。 更具体地说,在我的电子邮件模板中,我希望有如下内容: 其中“myBean”是一个@组件。这可能吗?我不是在寻找像这样的解决方法 其中bean作为变量添加到模

  • 问题内容: 我想使用Django模板发送HTML电子邮件: 我找不到任何有关的信息,而django-mailer仅发送HTML模板,而没有动态数据。 如何使用Django的模板引擎生成电子邮件? 问题答案: 从docs,要发送HTML电子邮件,你想使用其他内容类型,如下所示: 你可能需要两个用于电子邮件的模板-一个看起来像这样的纯文本模板,存储在你的模板目录下: 还有一个HTMLy,存放在以下位置

  • 我们目前正在为SpringMVC网站使用Thymeleaf。最近,我们决定使用它来处理丰富的HTML电子邮件模板。我在这里举了一个例子:http://www.thymeleaf.org/doc/springmail.html. ClassLoaderTemplateResolver的前缀为“email/”,模板存储在“src/main/resources/”下的“email”子文件夹中,该文件夹似

  • 问题内容: 我们有一个需要发送各种不同类型的模板电子邮件的应用程序。当前的代码非常麻烦且不够灵活。有没有一个图书馆可以帮助您完成此类工作…我们正在寻找某种用于电子邮件的模板库。 问题答案: StringTemplate还是一个非常好的模板引擎。