如何在Spring Boot中重定向FTL?

时间:2018-08-10 07:26:26

标签: spring spring-boot freemarker

我已经在春季启动中创建了一个电子邮件确认,它运行良好,并且当用户单击该链接时应该显示“已确认”时我也创建了一个链接,但是我不知道该怎么做?

电子邮件发件人java类:

@Component
public class EmailSender {

    @Autowired 
    JavaMailSender javaEmailSender;
    public void sendEmail(String to, String subject, String text) throws MessagingException {
         MimeMessage message = javaEmailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(message,
                    MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
                    StandardCharsets.UTF_8.name());
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text, true);
        helper.addInline("logo.jpg", new ClassPathResource("./images/logo.jpg"));
        javaEmailSender.send(message);
    }


}

电子邮件模板加载程序:

@Component
public class EmailTemplateLoader {

    @Autowired
    private Configuration freemakerConfiguration;

    public String getEmailBody(String name, String confirmationLink) throws TemplateException {

        try {
            Template template = freemakerConfiguration.getTemplate("EmailConfirmation.ftl");

            Map<String, Object> data = new HashMap<String, Object>();
            data.put("name", name);
            data.put("confirmationLink", confirmationLink);

            return FreeMarkerTemplateUtils.processTemplateIntoString(template, data);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return "";
    }
}

我的注册资源:

@Autowired
    private SignupService signupService;
    @Autowired
    private HttpServletRequest httpServletRequest;

    @RequestMapping(value = "user/signup", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<?> signup(@RequestBody UserType user) throws SignUpException {
        URL requestUrl = null;
        try {
            requestUrl = new URL(httpServletRequest.getRequestURL().toString());
        } catch (MalformedURLException e) {
            logger.debug("Malformed Request Url");
        }
        logger.debug(requestUrl.toString());
        signupService.signUp(user, requestUrl.getHost());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "user/confirmation", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<?> confirmSignUp(@RequestParam("u") String loginName, @RequestParam("p") String token) {
        try {
            signupService.emailConfirmation(loginName, token);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (SignUpException e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

所以,正如我告诉我的那样,我可以成功发送电子邮件,但是我无法对如何成功发送确认邮件进行排序

0 个答案:

没有答案