spring boot 使用FreeMarker模板
来源:互联网 发布:淘宝扣分12分会怎么样 编辑:程序博客网 时间:2024/04/30 11:34
在spring boot中使用FreeMarker模板非常简单方便,只需要简单几步就行:
1、引入依赖:
<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>其中,${time?date}表示time是日期类型的变量,只取date部分。“?date”还可以使用“?datetime”或“?time”。
3、使用模板,测试用例:
@AutowiredConfiguration configuration; //freeMarker configuration@Testpublic 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 templateString 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><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的值为空,则用空格代替。感叹号后面也可以加一个缺省字符串,在空值的时候代替空值。如果不加感叹号会报错。
1 2
- spring boot 使用FreeMarker模板
- spring boot 使用FreeMarker模板
- Spring boot 使用freemarker模板
- (9) spring boot使用freemarker模板引擎
- 93.Spring Boot使用模板freemarker【从零开始学Spring Boot】
- Spring Boot入门教程-FreeMarker模板
- Spring boot配置使用Freemarker
- Spring boot配置freemarker,jsp,velocity模板
- 《spring-boot学习》-14-spring boot整合freeMarker模板
- spring mvc 使用Freemarker模板
- spring boot 使用velocity、freeMarker模板创建html页面返回给前端
- (18)使用模板(thymeleaf-freemarker)【从零开始学Spring Boot】
- Spring Boot 使用 FreeMarker 渲染页面
- spring boot 配置freemarker及使用freemarker渲染页面
- Spring Boot基础6-web应用开发-模板引擎FreeMarker
- spring-boot配置html模板引擎freemarker、thymeleaf
- Spring boot freemarker模板路径的几种设置方式
- Spring Boot整合模板引擎freemarker以及servlet
- 挪威石油基金将起诉大众汽车
- EventBus 学习笔记
- 深入理解HashMap(及hash函数的真正巧妙之处)
- GitHub 优秀的 Android 开源项目
- 表的查询(二)
- spring boot 使用FreeMarker模板
- Java以DOM方式解析生成xml文件或字符串
- 请问腾迅前端高级开发工程师
- android的自定义toolbar
- pool(一)——入门
- dot实现HuffmanTree动态可视化
- dubbo注册服务IP解析异常及IP解析源码分析
- 【BOM操作】JavaScript中的event对象之总结
- iOS 通知中心传值