当前位置: 首页 > 知识库问答 >
问题:

Spring Boot调用thymeleaf模板失败

曾宏毅
2023-03-14

我得到了这个错误,我在堆栈上搜索了我的相同问题,我发现我不应该在调用它时放. 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;
    }

我从Rest服务中心打电话给它:

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

    shoppingCartService.clearShoppingCart(shoppingCart);

我正在放文件。项目中包下的html

共有2个答案

宗政昱
2023-03-14

实际上,当我的配置被放入我的问题中时,我只是把。html文件下的文件称为模板下的资源和它的工作邮件被发送,Spring开机显然是自动配置与此路径没有配置templateResolver

楚意
2023-03-14

在你的问题中,TemplateEngine是自动连接的,因此我看不到它是如何配置的,但它是为了从位置com发现你的模板。书店领域安全模板配置应如下所示:

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

在这段代码中,我正在代码中配置模板引擎,可能您正在使用XML。无论您如何配置模板引擎,很明显您是在使用Spring来实现这一点(因为您将其注入到您的邮件构造函数中),这里的关键点是,无论您如何配置它,您都需要告诉它在哪里找到您的模板,并且方法是调用ITemplateResolver的setPrefix()方法。

在Thymeleaf docs中,有一篇题为“在Spring用Thymeleaf发送电子邮件”的文章提供了更多详细信息。

 类似资料:
  • 我想在HTML中显示一个请求的对象,我得到了一个错误,我不知道是什么原因导致了我的错误。谷歌帮不上忙,现在我试着问你。我认为错误不是来自我的表,因为我把它注释掉了,错误仍然是Occour。该错误也不是来自“http://localhost:8081/simulation”,因为我使用有效值重新接收了一个有效的JSON。谢谢你的帮助:)。 下面是我的代码: 我通过调用“http://localhos

  • 本文向大家介绍SpringBoot中的Thymeleaf模板,包括了SpringBoot中的Thymeleaf模板的使用技巧和注意事项,需要的朋友参考一下 一、前言     Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1、JSP 最明显的问题在于它看起来像HTML或XML,但它其实上并不是。大多数的JS

  • 本文向大家介绍springboot中thymeleaf模板使用详解,包括了springboot中thymeleaf模板使用详解的使用技巧和注意事项,需要的朋友参考一下 这篇文章将更加全面详细的介绍thymeleaf的使用。thymeleaf 是新一代的模板引擎,在spring4.0中推荐使用thymeleaf来做前端模版引擎。 thymeleaf介绍 简单说, Thymeleaf 是一个跟 Vel

  • 本文向大家介绍SpringBoot使用thymeleaf模板过程解析,包括了SpringBoot使用thymeleaf模板过程解析的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了SpringBoot使用thymeleaf模板过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.导入依赖 2.application.yml文件中新

  • 本文向大家介绍springBoot加入thymeleaf模板的方式,包括了springBoot加入thymeleaf模板的方式的使用技巧和注意事项,需要的朋友参考一下 1.新建springBoot项目 在前面有两种方式 2.加入thymeleaf模板引擎 SpringBoot推荐使用thymeleaf模板引擎 语法简单,功能更强大 要想引入thymeleaf,只需要在pom,xml文件中加入如下依

  • 我们正在生产出html模板的pdf发票。 但是当html文本是中文的时候,结果就有问题了。 尽管使用UTF-8进行了设置,但特殊字符不会显示出来。 和模板解析器: