我得到这个错误我搜索了相同的问题,如我的堆栈,我有foudn,我不应该把.html调用它但我得到同样的错误:
`Caused by: org.thymeleaf.exceptions.TemplateInputException: Error resolving template "orderConfirmationEmailTemplate", template might not exist or might not be accessible by any of the configured Template Resolver`s
我的邮件构造函数:
@Component
public class MailConstructor {
@Autowired
private Environment env;
@Autowired
private TemplateEngine templateEngine;
public SimpleMailMessage constructNewUserEmail(User user, String password) {
String message="\nPlease use the following credentials to log in and edit your personal information including your own password."
+ "\nUsername:"+user.getUsername()+"\nPassword:"+password;
SimpleMailMessage email = new SimpleMailMessage();
email.setTo(user.getEmail());
email.setSubject("Le's Bookstore - New User");
email.setText(message);
email.setFrom(env.getProperty("support.email"));
return email;
}
public MimeMessagePreparator constructOrderConfirmationEmail (User user, Order order, Locale locale) {
Context context = new Context();
context.setVariable("order", order);
context.setVariable("user", user);
context.setVariable("cartItemList", order.getCartItemList());
String text = templateEngine.process("orderConfirmationEmailTemplate.html", context);
MimeMessagePreparator messagePreparator = new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper email = new MimeMessageHelper(mimeMessage);
email.setTo(user.getEmail());
email.setSubject("Order Confirmation - "+order.getId());
email.setText(text,true);
email.setFrom(new InternetAddress("alaaeddinezammel1993@gmail.com"));
}
};
return messagePreparator;
}
我从休息服务中调用它:
mailSender.send(mailConstructor.constructOrderConfirmationEmail(user, order, Locale.ENGLISH));
shoppingCartService.clearShoppingCart(shoppingCart);
并将文件.html放在项目包
下任何帮助,将不胜感激
答案 0 :(得分:1)
在您的问题中,TemplateEngine
是自动连线的,因此我无法看到它是如何配置的,但为了从位置com.bookstore.domain.security.templates
发现您的模板,配置应如下所示:
@Bean
public TemplateEngine templateEngine() {
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(templateResolver());
return templateEngine;
}
private ITemplateResolver templateResolver() {
final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix(“/com/bookstore/domain/security/templates“);
templateResolver.setSuffix(".html");
…
return templateResolver;
}
在这段代码中,我在代码中配置TemplateEngine
,也许你正在使用XML。无论您如何配置TemplateEngine,您显然都在使用Spring(因为您将其注入MailConstructor
),这里的关键点是无论如何您配置它您需要告诉它在哪里找到您的模板,并且这样做的方法是调用ITemplateResolver
的{{1}}方法。
Thymeleaf文档中标题为Sending email in Spring with Thymeleaf的文章中有更多详细信息。
答案 1 :(得分:0)