Spring Boot使用Thymeleaf作为模板发送电子邮件-配置不起作用
我有一个基于Spring Boot" title="Spring Boot">Spring Boot(最新的1.1.5.RELEASE)和Thymeleaf的WebApp。
现在,我想添加功能来发送电子邮件并将Thymeleaf用作模板引擎。
在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配置,如下所示:
@Configurationpublic 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:解决模板“登录”时出错,模板可能不存在,或者任何配置的模板解析器都无法访问该模板,
我尝试将不同的前缀放入webTemplateResolver.setPrefix,但未成功。
我也注意到Thymeleaf的早期版本中报告了错误,导致该错误,但看起来已修复,并且我具有较新的版本。
回答:
主要问题是您配置过多。
Spring
Boot已经配置了TemplateEngine
和ThymeleafViewResolver
。看到的ThymeleafAutoConfiguration
。如果您查看该类,您还将注意到它会自动检测ITemplateResolver
您的应用程序中可能包含的任何实例,并将其添加到中SpringTemplateEngine
。
解决方案非常简单,只需删除电子邮件配置和即可删除所有内容emailTemplateResolver
。其他一切将由Spring Boot处理。
@Configurationpublic 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;
}
}
以上是 Spring Boot使用Thymeleaf作为模板发送电子邮件-配置不起作用 的全部内容, 来源链接: utcz.com/qa/411963.html