Если вы используете весенний и тимьяновый лист, то они должны работать на вас как заклинание.В этом случае это просто:
private static final VAR_USER = "user"
@Autowired
private SpringTemplateEngine templateEngine;
...
public void method(User user,...) {
Map<String, Object> variables;
variables.put(VAR_USER, user);
context.setVariables(variables);
org.thymeleaf.context.Context context = new Context(locale);
String evaluated = templateEngine.process("myTemplate", context);
}
, где myTemplate относится к resources/mails/myTemplate.html
, а его содержимое выглядит следующим образом:
<p th:text="#{email.userActivity.greeting}">Hello</p>
<p th:text="#{email.userActivity.text1}">Following user activity...</p>
<ul>
...
<li th:text="#{email.userActivity.phone(${user.phoneNumber}?: #{error.not.provided})}">Phone number:</li>
<li th:text="#{email.userActivity.membershipNumber(${user.membershipNumber}?: #{error.not.provided})}">Membership number:</li>
...
</ul>
<p th:text="#{email.userActivity.text2}">Thanks for taking care of this demand within the agreed period!</p>
<p th:text="#{email.userActivity.text3}">Regards</p>
и моя User
сущность
public class User implements Serializable {
...
@Column(name = "membership_number")
private String membershipNumber;
@Column(name = "phone_number")
private String phoneNumber;
...
}
Затем моя конфигурация Thymeleaf:
package my.package.config;
import my.package.MyTemplateEngine;
import org.apache.commons.lang3.CharEncoding;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.StringTemplateResolver;
@Configuration
public class ThymeleafConfiguration {
private MyTemplateEngine templateEngine;
@Bean
@Description("Thymeleaf template resolver serving HTML 5 emails from template file")
public ITemplateResolver htmlTemplateResolver() {
ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
emailTemplateResolver.setPrefix("mails/");
emailTemplateResolver.setSuffix(".html");
emailTemplateResolver.setTemplateMode(TemplateMode.HTML);
emailTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
emailTemplateResolver.setCheckExistence(true);
return emailTemplateResolver;
}
@Description("Thymeleaf template resolver serving HTML 5 emails from input string")
@Bean
public ITemplateResolver stringTemplateResolver() {
final StringTemplateResolver templateResolver = new StringTemplateResolver();
templateResolver.setTemplateMode(TemplateMode.HTML);
return templateResolver;
}
@Bean
public MyTemplateEngine createTemplateEngine() {
templateEngine = new MyTemplateEngine();
return templateEngine;
}
}
и используемая мной версия Thymeleaf:
<properties>
...
<thymeleaf-layout-dialect.version>2.2.1</thymeleaf-layout-dialect.version>
<thymeleaf.version>3.0.6.RELEASE</thymeleaf.version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
...
</dependencies>