magomi
magomi

Reputation: 6685

Multiple template resolvers for thymeleaf on spring boot

I'm looking for a way to define two template resolvers that can be used for thymeleaf mail processing in a spring boot app. I need this because I have a html template and a text template. Both are necessary to provide rich text and plain text content in the email.

All configuration shall be done in application.properties or via environment properties.

I've only managed to define one template resolver:

spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/mails/
spring.thymeleaf.excluded-view-names=
spring.thymeleaf.view-names=
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=true

I would be glad if anyone could give me a hint or show me the right direction where to search for a solution.

Upvotes: 8

Views: 10253

Answers (1)

admirm
admirm

Reputation: 91

had the same topic and solved it thanks to the thymeleaf site. Visit http://www.thymeleaf.org/doc/articles/springmail.html

Here is also a sample of the configuration:

https://github.com/thymeleaf/thymeleafexamples-springmail/blob/3.0-master/src/main/java/thymeleafexamples/springmail/business/SpringMailConfig.java

The main method that you should look into is this one:

/* ******************************************************************** */
/*  THYMELEAF-SPECIFIC ARTIFACTS FOR EMAIL                              */
/*  TemplateResolver(3) <- TemplateEngine                               */
/* ******************************************************************** */

@Bean
public TemplateEngine emailTemplateEngine() {
    final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    // Resolver for TEXT emails
    templateEngine.addTemplateResolver(textTemplateResolver());
    // Resolver for HTML emails (except the editable one)
    templateEngine.addTemplateResolver(htmlTemplateResolver());
    // Resolver for HTML editable emails (which will be treated as a String)
    templateEngine.addTemplateResolver(stringTemplateResolver());
    // Message source, internationalization specific to emails
    templateEngine.setTemplateEngineMessageSource(emailMessageSource());
    return templateEngine;
}

Here are defined multiple template resolvers.

The con part is, that is java code and it is not handled over the application.properties way. If you find any way to define them in the application.properties ... leave a comment.

Upvotes: 3

Related Questions