gpt4 book ai didi

Java Spring Boot 和 Thymeleaf : An error happened during template parsing

转载 作者:行者123 更新时间:2023-12-01 16:14:34 26 4
gpt4 key购买 nike

我正在开发一个 Java Spring Boot Web 应用程序,用户可以在其中注册。当他们注册时,他们会收到一封验证电子邮件。当我尝试获取电子邮件中的链接以实际确认注册时,这不再起作用,因为我收到错误:“模板解析期间发生错误(模板:“mail/verifyEmail.html”)”。我不知道为什么会这样。我在下面附加了 verifyEmail.html 文件,然后附加了附带的 EmailService.java 文件,其中包含 EmailService() 和 sendVerificationEmail() 方法:

<!DOCTYPE html>
<html>

<th:block xmlns:th="http://www.thymeleaf.org">

<span th:text="'Hello ' + ${name}"></span>

<p>Thank you for registering for the bcoreHW site!</p>
<p>Please click <a href="#">here</a> to verify your email address.</p>

</th:block>

</html>
import java.util.Date;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

@Service
public class EmailService {
@Autowired
private JavaMailSender mailSender;

@Value("${mail.enable}")
private boolean enable;

private TemplateEngine templateEngine;

private void send(MimeMessagePreparator preparator) {
if(enable) {
mailSender.send(preparator);
}
}

@Autowired
public EmailService(TemplateEngine templateEngine) {

ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("mail/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCacheable(false);
templateEngine.setTemplateResolver(templateResolver);

this.templateEngine = templateEngine;
}

public void sendVerificationEmail(final String emailAddress) {

Context context = new Context();
context.setVariable("name", "Bob");

final String emailContents = templateEngine.process("verifyEmail", context);

System.out.println(emailContents);

MimeMessagePreparator preparator = new MimeMessagePreparator() {

// not working for some reason: @Override
public void prepare(MimeMessage mimeMessage) throws Exception {

MimeMessageHelper message = new MimeMessageHelper(mimeMessage);

message.setTo(emailAddress);
message.setFrom(new InternetAddress("no-reply@testSite.com"));
message.setSubject("Please Verify Your Email Address");
message.setSentDate(new Date());
message.setText(emailContents, true);
}
};

send(preparator);
}
}

有人发现这有什么问题吗?谢谢。另外,这是我收到的错误消息:

Caused by: java.io.FileNotFoundException: ClassLoader resource "mail/verifyEmail.html" could not be resolved
at org.thymeleaf.templateresource.ClassLoaderTemplateResource.reader(ClassLoaderTemplateResource.java:130) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:223) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]

最佳答案

我发现了这个问题。我的前缀“mail/”不正确,因为它实际上位于名为“mailTemplates”的目录中。我真傻。

关于Java Spring Boot 和 Thymeleaf : An error happened during template parsing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62439863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com