在spring boot中使用FreeMarker模板非常简单方便,只需要简单几步就行:

1、引入依赖:



 

  1. <dependency>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-freemarker</artifactId>  
  4. </dependency>  
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>


2、创建模板:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <body>  
  4. <h4>亲爱的${toUserName},你好!</h4>  
  5.   
  6. <p style="color:blue;"> ${message}</p>  
  7.   
  8. 祝:开心!  
  9. </br>  
  10. ${fromUserName}  
  11. </br>  
  12. ${time?date}  
  13.   
  14. </body>  
  15. </html>  
<!DOCTYPE html>
<html>
<body>
<h4>亲爱的${toUserName},你好!</h4>
<p style="color:blue;"> ${message}</p>
祝:开心!

</br>

${fromUserName}

</br>

${time?date}
</body>

</html>

其中,${time?date}表示time是日期类型的变量,只取date部分。“?date”还可以使用“?datetime”或“?time”。




3、使用模板,测试用例:


 

  1. @Autowired  
  2. Configuration configuration; //freeMarker configuration  
  3.   
  4. @Test  
  5. public void sendHtmlMailUsingFreeMarker() throws Exception {  
  6.     Map<String, Object> model = new HashMap<String, Object>();  
  7.     model.put("time", new Date());  
  8.     model.put("message", "这是测试的内容。。。");  
  9.     model.put("toUserName", "张三");  
  10.     model.put("fromUserName", "老许");  
  11.       
  12.     Template t = configuration.getTemplate("welcome.ftl"); // freeMarker template  
  13.     String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);  
  14.   
  15.     logger.debug(content);  
  16.     //mailService.sendHtmlMail(to, "主题:html邮件", content);  
  17. }  
@Autowired
Configuration configuration; //freeMarker configuration
@Test</br>
public void sendHtmlMailUsingFreeMarker() throws Exception {</br>
Map<String, Object> model = new HashMap<String, Object>();</br>
model.put("time", new Date());</br>
model.put("message", "这是测试的内容。。。");</br>
model.put("toUserName", "张三");</br>
model.put("fromUserName", "老许");</br></br>

Template t = configuration.getTemplate("welcome.ftl"); // freeMarker template</br>
String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);</br></br>

logger.debug(content);</br>
//mailService.sendHtmlMail(to, "主题:html邮件", content);</br>
}</pre><br>

4、测试结果:

源代码参考:https://github.com/xujijun/my-spring-boot
FreeMarker官网:http://freemarker.org/
附:使用循环遍历一个List的模板:



[html] view plain copy
print?

1.

2. <html>
3. <body>
4. <h3>发现错误!<a href="${errorLogLink}" target="_blank">点击这里查看详情</a></h3>
5.
6. <h3>错误列表:</h3>
7.
8. <table border="1px solid #8968CD" style="border-collapse: collapse;"><tr><th>错误位置</th> <th>数量</th> <th>错误信息</th> <th>错误类名</th> <th>更多信息</th> </tr>
9. <#list errorList as error>
10. <tr>
11. <td>${error.pos}</td>
12. <td>${error.count}</td>
13. <td>${error.msg}</td>
14. <td>${error.eName!}</td>
15. <td>${error.details!}</td>
16. </tr>
17. </#list>
18. </table>
19.
20. </body>
21. </html>
22.



<html>
<body>
<h3>发现错误!<a href="${errorLogLink}" target="_blank">点击这里查看详情</a></h3>
<h3>错误列表:</h3>

<table border="1px solid #8968CD" style="border-collapse: collapse;"><tr><th>错误位置</th> <th>数量</th> <th>错误信息</th> <th>错误类名</th> <th>更多信息</th> </tr>
<#list errorList as error>
<tr>
<td>${error.pos}</td>
<td>${error.count}</td>
<td>${error.msg}</td>
<td>${error.eName!}</td>
<td>${error.details!}</td>
</tr>
</#list>
</table>

</body>

</html>
注意:最后那个两个感叹号表示:如果error.eName/error.details的值为空,则用空格代替。感叹号后面也可以加一个缺省字符串,在空值的时候代替空值。如果不加感叹号会报错。