Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 757
magomi Avatar asked Dec 08 '16 09:12

magomi


1 Answers

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.

like image 69
admirm Avatar answered Oct 31 '22 22:10

admirm