如何使用上下文将参数传递给Thymeleaf模板?

时间:2015-06-30 20:56:51

标签: spring thymeleaf

我正在尝试发送HTML电子邮件。我已经创建了一个模板文件,但由于它是由另一个模板解析器解析的,我无法弄清楚如何将我的参数传递给模板。

我有模板解析器和引擎的以下配置:

@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;
}

我将模板中所需的所有参数从控制器传递到服务。以下是我发送电子邮件的服务实现:

@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);
  }
}

以下是我想要使用参数的模板部分:

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

有没有办法做到这一点,还是我还需要别的东西?

提前致谢

1 个答案:

答案 0 :(得分:2)

Jail,我希望我理解正确,你希望将参数传递给页面来呈现它们,在这种情况下希望这会有所帮助。 你很近所有你要做的就是从上下文调用setVariable: http://www.thymeleaf.org/apidocs/thymeleaf/2.0.2/org/thymeleaf/context/AbstractContext.html#setVariables(java.util.Map)

将您的模板视为标准的百万美元页面。

所以使用你的标题示例:

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

记住:值只会设置值属性,th:text会改变我认为你所追求的标题(否则继续使用th:value)。

在你的java代码中添加这一行

// 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 */);