Thymeleaf TemplateEngine无法处理远程服务器上的模板

时间:2019-03-06 11:09:13

标签: java spring-boot thymeleaf javamail

我正在尝试从SpringBoot应用程序中使用Thymeleaf发送电子邮件。使用以下方法在本地处理模板:

public String build(MailType mailType, Map<String, Object> messageMap) {

    Context context = new Context();

    context.setVariables(messageMap);
    return templateEngine.process("/mail/" + mailType.name(), context);

}

MailType是一个枚举,其中存储了每个模板的名称,例如NEWUSER。邮件模板存储在src / main / resources / templates / mail /

这会在本地运行时按预期发送电子邮件,但是在服务器上运行时,我会收到以下错误消息:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/mail/NEWUSER], template might not exist or might not be accessible by any of the configured Template Resolvers

有人对此有任何想法吗?任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:0)

Hard-coding the plain text /mail/ was the problem. Used a TemplateResolver and this fixed the issue.

context.setVariables(messageMap);

    TemplateEngine templateEngine = new TemplateEngine();

    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setPrefix("templates/mail/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(TemplateMode.HTML);
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setOrder(0);

    templateEngine.setTemplateResolver(templateResolver);

(May not be best practice, any better ideas?)

答案 1 :(得分:0)

You have not provide suffix in above case.Don't you have html pages in static directory for processing. Please show the html page that you want to use for email that might be you have put in /mail/ directory.

Thanks, Vishal

答案 2 :(得分:0)

执行与@matgr相同的操作,但不要忘记将空字符串传递给templateEngine.proccess方法,如下所示。

return templateEngine.process("", context);