java – 使用Thymeleaf作为模板发送电子邮件的Spring启动 – 配置不起作用

前端之家收集整理的这篇文章主要介绍了java – 使用Thymeleaf作为模板发送电子邮件的Spring启动 – 配置不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在基于 Spring Boot(最新的1.1.5.RELEASE)和Thymeleaf的WebApp工作.
现在我想添加发送电子邮件功能,并使用Thymelaf作为模板引擎.

在pom.xml中我添加

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-mail</artifactId>
</dependency>

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

遵循本教程:http://www.thymeleaf.org/doc/articles/springmail.html

得到完整的(没有XML)Java配置:

@Configuration
public ThymeleafReplaceConfigurator {
  @Bean 
   public JavaMailSender getJavaMailSenderImpl(){
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        Properties props = new Properties();
        /* some properties here */

        javaMailSender.setJavaMailProperties(props);

    return javaMailSender;
    }

    @Bean
    public ClassLoaderTemplateResolver emailTemplateResolver(){
        ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
        emailTemplateResolver.setPrefix("/mails/");
        emailTemplateResolver.setSuffix(".html");
        emailTemplateResolver.setTemplateMode("HTML5");
        emailTemplateResolver.setCharacterEncoding("UTF-8");
        emailTemplateResolver.setOrder(1);

        return emailTemplateResolver;
    }

    @Bean
    public ServletContextTemplateResolver defaultWebTemplateResolver(){
        ServletContextTemplateResolver webTemplateResolver = new ServletContextTemplateResolver();
        webTemplateResolver.setPrefix("/templates/");
        webTemplateResolver.setSuffix(".html");
        webTemplateResolver.setTemplateMode("HTML5");
        webTemplateResolver.setCharacterEncoding("UTF-8");
        webTemplateResolver.setOrder(2);

        return webTemplateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine(){
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(emailTemplateResolver());
    templateEngine.setTemplateResolver(defaultWebTemplateResolver());
    return templateEngine;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();

        thymeleafViewResolver.setTemplateEngine(templateEngine());
        thymeleafViewResolver.setCharacterEncoding("UTF-8");

        return thymeleafViewResolver;
    }
}

文件夹和文件树如下:

src
  main
    resources
      templates
        login.html
        error.html
      mails
        exampleMail.html

但它不起作用.应用程序正确启动但访问页面(没有此配置工作)会导致异常,例如:

请求处理失败;嵌套异常是org.thymeleaf.exceptions.TemplateInputException:解析模板“login”时出错,模板可能不存在,或者任何已配置的模板解析器都可能无法访问
我试图将不同的前缀放入webTemplateResolver.setPrefix但没有成功.

此外,我注意到在早期版本的Thymeleaf中报告的错误导致了但看起来它已修复,我有更新的版本.
有没有人在这种配置中看到misteake?

@H_403_25@

解决方法

主要问题是你配置太多了.

Spring Boot已经配置了TemplateEngine和ThymeleafViewResolver.请参阅ThymeleafAutoConfiguration.如果您是look at that class,您还会注意到它将自动检测您的应用程序中可能包含的任何ITemplateResolver实例,并将其添加到SpringTemplateEngine中.

除了电子邮件配置和emailTemplateResolver之外,解决方案非常简单.其他所有内容都将由Spring Boot处理.

@Configuration
public class ThymeleafEmailConfiguration {
   @Bean 
   public JavaMailSender getJavaMailSenderImpl(){
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        Properties props = new Properties();
        /* some properties here */

        javaMailSender.setJavaMailProperties(props);

        return javaMailSender;
    }

    @Bean
    public ClassLoaderTemplateResolver emailTemplateResolver(){
        ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
        emailTemplateResolver.setPrefix("/mails/");
        emailTemplateResolver.setSuffix(".html");
        emailTemplateResolver.setTemplateMode("HTML5");
        emailTemplateResolver.setCharacterEncoding("UTF-8");
        emailTemplateResolver.setOrder(1);

        return emailTemplateResolver;
    }
}
@H_403_25@ @H_403_25@

猜你在找的Java相关文章