Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a parameter to Thymeleaf template using context?

I'm trying to send an HTML email. I've created a template file, but since it is being resolved by a different template resolver, I couldn't figure out how to pass my parameters to the template.

I have the following configuration for the template resolvers and the engine:

@Bean
public TemplateResolver templateResolver() {
    TemplateResolver resolver = new ServletContextTemplateResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".html");
    resolver.setCharacterEncoding("UTF-8");
    resolver.setTemplateMode("HTML5");
    resolver.setOrder(2);
    return resolver;
}

@Bean
public TemplateResolver emailTemplateResolver() {
    TemplateResolver resolver = new ClassLoaderTemplateResolver();
    resolver.setPrefix("mail/");
    resolver.setSuffix(".html");
    resolver.setTemplateMode("HTML5");
    resolver.setCharacterEncoding("UTF-8");
    resolver.setOrder(1);
    return resolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
    final SpringTemplateEngine engine = new SpringTemplateEngine();
    final Set<TemplateResolver> templateResolvers = new HashSet<TemplateResolver>();
    templateResolvers.add(emailTemplateResolver());
    templateResolvers.add(templateResolver());
    engine.setTemplateResolvers(templateResolvers);
    return engine;
}

I'm passing all the parameters I need in my template from controller to service. Here is my service implementation for sending emails:

@Service("EmailService")
public class EmailServiceImpl implements EmailService {

  @Autowired
  private JavaMailSender mailSender;

  @Autowired
  private TemplateEngine templateEngine;

  public void sendSimpleMail(final Locale locale,
                           final String category,
                           final String recipientEmail)
        throws MessagingException {

      // Prepare the evaluation context
      final Context ctx = new Context(locale);

      // Prepare message using a Spring helper
      final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
      final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
      String subject = category + " - " + result_filter;
      message.setSubject(subject);
      message.setTo(recipientEmail);

      // Create the HTML body using Thymeleaf
      final String htmlContent = this.templateEngine.process("email", ctx);
      message.setText(htmlContent, true /* isHtml */);


      // Send email
      this.mailSender.send(mimeMessage);
  }
}

Here is the part of the template I want to use the parameter:

<label style="font-size: 12px;padding-bottom: 1em;" th:value="parameter">Test</label>

Is there a way to do this or do I need something else for this?

Thanks in advance

like image 392
Jail Avatar asked Jul 05 '26 12:07

Jail


1 Answers

Jail, I hope i understand correctly, you wish to pass parameters to page to render them in which case hope this helps. You are close all you have to do is call setVariable from context: http://www.thymeleaf.org/apidocs/thymeleaf/2.0.2/org/thymeleaf/context/AbstractContext.html#setVariables(java.util.Map)

Treat your template as a standard thymeleaf page.

so using your Title example:

<label style="font-size: 12px;padding-bottom: 1em;" th:text="${parameter}">Test</label>

Remember th:value will only set the value attribute, th:text will change the title which i think is what you are after (otherwise keep using th:value).

In your java code add this line

// This is where you set your variables
      ctx.setVariables("parameter","Random Title");  // here we are setting the parameter variable to "Random Title"

 // Create the HTML body using Thymeleaf
      final String htmlContent = this.templateEngine.process("email", ctx);
      message.setText(htmlContent, true /* isHtml */);
like image 155
Aeseir Avatar answered Jul 07 '26 04:07

Aeseir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!