springboot 使用FreeMarker模板(转)
2018-01-18 14:00
435 查看
在spring boot中使用FreeMarker模板非常简单方便,只需要简单几步就行:
1、引入依赖:
[html] view plain copy print?<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2、创建模板:
[html] view plain copy print?<!DOCTYPE html>
<html>
<body>
<h4>亲爱的${toUserName},你好!</h4>
<p style="color:blue;"> ${message}</p>
祝:开心!
</br>
${fromUserName}
</br>
${time?date}
</body>
</html>
3、使用模板,测试用例:
[java] view plain copy print?@Autowired
Configuration configuration; //freeMarker 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"); // freeMarker template
String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
logger.debug(content);
//mailService.sendHtmlMail(to, "主题:html邮件", content);
}
4、测试结果:
源代码参考:https://github.com/xujijun/my-spring-boot
FreeMarker官网:http://freemarker.org/
附:使用循环遍历一个List的模板:
[html] view plain copy print?<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>
1、引入依赖:
[html] view plain copy print?<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、创建模板:
[html] view plain copy print?<!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、使用模板,测试用例:
[java] view plain copy print?@Autowired
Configuration configuration; //freeMarker 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"); // freeMarker template
String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
logger.debug(content);
//mailService.sendHtmlMail(to, "主题:html邮件", content);
}
@Autowired Configuration configuration; //freeMarker 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"); // freeMarker template String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model); logger.debug(content); //mailService.sendHtmlMail(to, "主题:html邮件", content); }
4、测试结果:
源代码参考:https://github.com/xujijun/my-spring-boot
FreeMarker官网:http://freemarker.org/
附:使用循环遍历一个List的模板:
[html] view plain copy print?<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的值为空,则用空格代替。感叹号后面也可以加一个缺省字符串,在空值的时候代替空值。如果不加感叹号会报错。
相关文章推荐
- SpringBoot使用FreeMarker模板发送邮件
- java使用 freemarker 模板生成一个excel文件多个sheet
- 使用FreeMarker加载远程主机上模板文件,比如FTP,Hadoop等(转载)
- spring boot 使用FreeMarker模板
- 使用Java进行FreeMarker的web模板开发的基础教程
- spring mvc 使用Freemarker模板
- [Freemarker] - 使用struts的component调用freemarker的ftl模板方法
- Spring Boot使用模板freemarker【从零开始学Spring Boot(转)
- spring boot 使用FreeMarker模板
- spring boot 使用velocity、freeMarker模板创建html页面返回给前端
- Spring Boot 与 Kotlin使用Freemarker模板引擎渲染web视图
- 如何在freemarker模板里面使用struts2的标签
- 【java开发】使用freemarker开发模板应用
- 使用FreeMarker将数据模型中的值合并到模板文件中
- Spring 4 使用Freemarker模板发送邮件&添加附件
- 【freemarker】使用模板导出word
- 使用freemarker生成xml模板
- word导出(使用freemarker模板导出)
- 使用Spring发送基于freemarker模板的邮件
- Web 环境中使用 FreeMarker 模板引擎的配置方法