Şu satırı dahil ederiz
import org.thymeleaf.context.Context;import org.thymeleaf.spring5.SpringTemplateEngine;
addTemplateResolver metodu
Örnek
Şöyle yaparız
@Configuration public class TemplateEngineConfig implements WebMvcConfigurer { private static final String EMAIL_TEMPLATE_ENCODING = "UTF-8"; @Bean @Primary public TemplateEngine emailTemplateEngine() { final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); // Resolver for HTML emails (except the editable one) templateEngine.addTemplateResolver(emailTemplateResolver()); return templateEngine; } private ITemplateResolver emailTemplateResolver() { ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); templateResolver.setResolvablePatterns(Collections.singleton("*")); templateResolver.setPrefix("/templates/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode(TemplateMode.HTML); templateResolver.setCharacterEncoding(EMAIL_TEMPLATE_ENCODING); templateResolver.setCacheable(false); return templateResolver; } }
custom configuration kullandığımı için Thymelead auto configuration kapatılır. şöyle yaparız
@SpringBootApplication(exclude = { ThymeleafAutoConfiguration.class }) public class MailApplication { public static void main(String[] args) { SpringApplication.run(MailApplication.class, args); } }
process metodu - Path + Context
Örnek
/src/main/resources/templates/project-status-change.txt dosyası olsun. Dosyanın için şöyle olsun. Değişkenler [(${...})] şeklinde tanımlı
Hello [(${fullName})],Your subscription for [(${productName})] received an update. Please go check it out on :https://definitely-not-a-spam.com/legit-linkCheers.
Şöyle yaparız
ProjectStatusChangeDto projectStatus = ...,Context context = new Context();context.setVariable("fullName", projectStatus.getAuthorFullName());context.setVariable("productName", projectStatus.getProductName());SpringTemplateEngine templateEngine = ...;;String text = templateEngine.process("project-status-change.txt", context);
Hiç yorum yok:
Yorum Gönder