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

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

我正在尝试发送 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>

有没有办法做到这一点,或者我需要其他东西吗?

提前致谢

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

将您的模板视为标准的 thymeleaf 页面。

所以使用您的标题示例:

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

记住 th:value 只会设置值属性,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 */);