无法将变量设置为ELEAF模板

qfe3c7zg  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(257)

我必须发送电子邮件与thymeleaf模板内,但我收到了明确的html代码没有所需的参数(链接)。我的错在哪里?
方法:

private void sendLetter(User user, String subject, String link, String templatePath, Locale locale) {
    Context ctx = new Context(locale);
    ctx.setVariables(Collections.singletonMap("link", link));
    String htmlContent = templateEngine.process(templatePath, ctx);
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        message.setTo(user.getEmail());
        message.setFrom(emailProperties.getFrom());
        message.setSubject(subject);
        message.setText(htmlContent, true);
    }
    catch (MessagingException e) {
        e.printStackTrace();
    }

    javaMailSender.send(mimeMessage);
}

主题:

<a class="main_button" href="${link}">
9rbhqvlz

9rbhqvlz1#

你需要使用 th:href 而不是 href .
更改代码

<a class="main_button" href="${link}">

<a class="main_button" th:href="${link}">

相关问题