springmvc 实现 Javamail 踩坑 Configuration 没有实例化
事件的起因是这一行代码报错:
Template template = freeMarkerConfigurer.getConfiguration().getTemplate("mail.ftl");
没有getTemplate方法。
然后找了很多spring+Javamail的例子发现都是这样写的 没有什么问题,然后只有看源码了。
FreeMarkerConfigurer.java
/**
* Return the Configuration object wrapped by this bean.
*/
@Override
public Configuration getConfiguration() {
return this.configuration;
}
发现configuration是红的,没有相关的包,然后通过idea直接导入发现提示文件只读。
通过查看org.springframework.web.servlet.view.freemarker包里面什么里面的内容基本都不能用。
最后在这里找到一个引用。
需要在maven中添加一个freemarker的依赖:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
完整的依赖是这样:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>

浙公网安备 33010602011771号