Skip to content
Advertisement

Thymeleaf TemplateEngine unable to process templates on remote server

I am attempting to send emails using Thymeleaf from a SpringBoot application. Templates are processed locally using the following method:

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

    Context context = new Context();

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

}

MailType is an Enum with the names of each template stored, for example NEWUSER. Mail templates are stored in src/main/resources/templates/mail/

This sends an email as intended when running locally, but when running on the server, I receive the following error message:

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

Does anyone have any ideas about this? Any help would be greatly appreciated!

Advertisement

Answer

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?)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement