简体   繁体   English

Spring Boot调用百里香模板失败

[英]spring boot calling thymeleaf template failed

im getting this error i searched on the same issues like mine on stack and i have foudn that i shouldn t put .html when calling it but im getting the same error : 即时通讯收到此错误,我搜索了类似我的堆栈上的问题,我发现我不应该在调用它时放.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

my mail constructor : 我的邮件构造函数:

@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;
    }

and im calling it from rest service: 我从休息服务中调用它:

mailSender.send(mailConstructor.constructOrderConfirmationEmail(user, order, Locale.ENGLISH));

    shoppingCartService.clearShoppingCart(shoppingCart);

and im putting the file .html under package in the project 并即时将.html文件放在项目的包中

在此处输入图片说明

In your question, the TemplateEngine is auto wired so I cannot see how it is configured but it in order to discover your template from the location com.bookstore.domain.security.templates the configuration should look something like this: 在您的问题中, 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;
}

In this code I am configuring the TemplateEngine in code, perhaps you are using XML. 在此代码中,我正在代码中配置TemplateEngine ,也许您正在使用XML。 Regardless of how you are configuring the TemplateEngine, you are clearly using Spring to do so (since you inject it into your MailConstructor ), the key point here is that regardless of how you configure it you need to tell it where to find your template and the way to do that is to invoke the ITemplateResolver 's setPrefix() method. 无论您是如何配置TemplateEngine,你都清楚使用Spring这样做(因为你把它注射到你的MailConstructor ),这里的关键点是,不管你如何配置它,你需要告诉它在哪里可以找到你的模板,完成此操作的方法是调用ITemplateResolversetPrefix()方法。

Plenty more details in the article titled Sending email in Spring with Thymeleaf in the Thymeleaf docs. Thymeleaf文档中标题为“ 使用Thymeleaf春季发送电子邮件”的文章中有更多详细信息。

actually with my configuration putted in my question i just put the .html file under file called templates under resources and it works the mail is sent , spring boot is apparently auto configured with this path without configuring the templateResolver 实际上,我的问题中存在我的配置,我只是将.html文件放在资源下的名为template的文件下,并且可以正常发送邮件,显然使用此路径自动配置了spring boot,而无需配置templateResolver

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM