首页 > 解决方案 > Thymeleaf 无法识别服务的属性

问题描述

我正在开发两个电子邮件模板,一个将发送确认 URL 以验证用户帐户,另一个将发送重置密码链接。

激活账户

<p>Use <a th:href="${confirmationUrl}" target="_blank" rel="noopener">this link</a> to activate your account now</p>

重设密码

<a th:href="${resetPasswordUrl}" target="_blank">Reset password</a>

两个链接都接收th:href来自以下方法的属性:

此方法发送验证令牌链接以激活帐户

public void sendVerificationToken(User user) {
    String token = jwtTokenService.genEmailVerificationToken(user.getId());
    logger.info("Sending verification token to user. user={} email={} token={}",
            user.getId(), user.getEmail(), token);
    String confirmationUrl = null;
    try {
        confirmationUrl = new URIBuilder(verifyEmailUrl)
                .addParameter(VERIFY_EMAIL_ENDPOINT_PARAM, token)
                .build().toString();
        model.addAttribute("confirmationUrl", confirmationUrl);
    }

    catch (URISyntaxException e) {
        logger.error("URL stored as redirect url for verify email is not valid.");
        throw ErrorFactory.serverError("Error");
    }
    emailService.sendAccountConfirmationEmail(user.getEmail(), confirmationUrl);
}

此方法发送密码重置链接

public void resetPasswordRequest(User user) {
    logger.info("Request for password recovery. email={}",
            user.getEmail());
    String token = jwtTokenService.genPasswordResetToken(user.getId());
    String resetPasswordUrl = null;
    try {
        resetPasswordUrl = new URIBuilder(resetPasswordUrl)
                .addParameter(RESET_PASS_ENDPOINT_PARAM, token)
                .build().toString();
        model.addAttribute("resetPasswordUrl", resetPasswordUrl);
    }
    catch (URISyntaxException e) {
        logger.error("URL stored as target url for reset password email is not valid.");
        throw ErrorFactory.serverError("Error");
    }
    emailService.sendPasswordReset(user.getEmail(), resetPasswordUrl);
}

我试图在 HTML 模板中获取confirmationUrlresetPasswordUrl使用的方法是将模型参数声明为这两种方法之上的类级别属性,然后使用model.addAttribute("attributeName", attribute).

private Model model;

Cannot resolve 'resetPasswordUrl'但出于某种原因,我正在获取Cannot resolve 'confirmationUrl'模板文件。就像它不识别属性一样。

标签: javahtmlspringspring-bootthymeleaf

解决方案


我同意@Srinivasa Raghavan 所说的话。我认为没有必要在邮寄步骤中使用模型对象。

1 - 如果您发送确认 url 的方法像这样工作就足够了。您可以调整示例代码以获得更一般的用途。

public void sendAccountConfirmationEmail(String recipient, String confirmationUrl) {

        Context context = new Context();
        context.setVariable("confirmationUrl", confirmationUrl);

        MimeMessagePreparator mimeMessagePreparator = mimeMessage -> {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, "UTF-8");
            messageHelper.setFrom("Test <test@test.com>");
            messageHelper.setTo(recipient);
            messageHelper.setSubject("Active Account");
            String content = templateEngine.process("your_active_account.html", context);            
            messageHelper.setText(content, true);
        };

        mailSender.send(mimeMessagePreparator);
    }

2 - 为了处理我们的模板,您将在我们的 Spring 电子邮件配置中配置一个专门为电子邮件处理配置的 TemplateEngine。您可以参考thymeleaf 文档来创建TemplateEngine.


推荐阅读