Java – spring launch using thymeleaf as a template to send e-mail – configuration does not work

I work on webapp based on spring boot (the latest 1.1.5. Release) and thymeleaf

In POM XML I add:

<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>

Follow this tutorial: http://www.thymeleaf.org/doc/articles/springmail.html

Get a complete (no XML) Java configuration:

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

The folder and file tree are as follows:

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

But it doesn't work The application starts correctly, but accessing the page (without this configuration work) will cause exceptions, such as:

Request processing failed; The nested exception is org thymeleaf. exceptions. Templateinputexception: error parsing template 'Login'. The template may not exist, or any configured template parser may not be accessible. I tried to put a different prefix into webtemplateresolver Setprefix failed

In addition, I noticed an error reported in an earlier version of thymeleaf, but it seems that it has been fixed. I have an updated version Does anyone see misteake in this configuration?

Solution

The main problem is that you have too many configurations

Spring boot has been configured with templateengine and thymeleafviewresolver See thymeleafautoconfiguration If you are look at that class, you will also notice that it will automatically detect any itemplateresolver instances that may be included in your application and add them to the springtemplate engine

In addition to email configuration and emailtemplateresolver, the solution is very simple Everything else will be handled by 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;
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>