在spring boot中使用FreeMarker模板非常簡單方便,只需要簡單幾步就行:
1、引入依賴:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-freemarker</artifactId>
- </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2、創建模板:
- <!DOCTYPE html>
- <html>
- <body>
- <h4>親愛的${toUserName},你好!</h4>
-
- <p style="color:blue;"> ${message}</p>
-
- 祝:開心!
- </br>
- ${fromUserName}
- </br>
- ${time?date}
-
- </body>
- </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、使用模板,測試用例:
- @Autowired
- Configuration configuration;
-
- @Test
- public void sendHtmlMailUsingFreeMarker() throws Exception {
- Map<String, Object> model = new HashMap<String, Object>();
- model.put("time", new Date());
- model.put("message", "這是測試的內容。。。");
- model.put("toUserName", "張三");
- model.put("fromUserName", "老許");
-
- Template t = configuration.getTemplate("welcome.ftl");
- String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
-
- logger.debug(content);
-
- }
@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>
- <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>
<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的值為空,則用空格代替。感嘆號后面也可以加一個缺省字符串,在空值的時候代替空值。如果不加感嘆號會報錯。