springboot 使用FreeMarker模板(转)
在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; //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</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的值为空,则用空格代替。感叹号后面也可以加一个缺省字符串,在空值的时候代替空值。如果不加感叹号会报错。
springboot 使用FreeMarker模板(转)的更多相关文章
- SpringBoot获取Freemarker模板引擎,生成HTML代码
今天用Ajax异步添加评论,加载Freemarker模板引擎,生成模板模块 1.新建Freemarker模板 <li id="${comment.oId}"> < ...
- SpringBoot使用freemarker模板
导入依赖 <!-- 添加freemarker模版的依赖 --> <dependency> <groupId>org.springframework.boot< ...
- springboot整合freemarker模板引擎后在页面获取basePath绝对路径
在项目中引用静态资源文件或者进行ajax请求时我们有时候会使用 ${basePath} ,其实这就是一种获取绝对路径的方式: 那么在springboot项目中要怎么配置才能使用 basePaht呢? ...
- SpringBoot整合freemarker模板
一.目录展示 二.导入依赖 三.application.properties配置文件 四.在src/main/resource/templates文件夹中创建HelloFreeMarker.ftl文件 ...
- springboot之freemarker 和thymeleaf模板web开发
Spring Boot 推荐使用Thymeleaf.FreeMarker.Velocity.Groovy.Mustache等模板引擎.不建议使用JSP. 一.Spring Boot 中使用Thymel ...
- 使用 FreeMarker模板 Springboot 发送邮件
四.使用 FreeMarker模板 HTML 标签的字符串拼接是一件很棘手的事.因为在你的大脑中解析HTML标签并想象它在渲染时会是什么样子是挺困难的.而将HTML混合在Java代码中又会使得这个问题 ...
- SpringBoot集成freemarker和thymeleaf模板
1.在MAVEN工程POM.XML中引入依赖架包 <!-- 引入 freemarker 模板依赖 --> <dependency> <groupId>org.spr ...
- SpringBoot基础及FreeMarker模板
案例springboot_freemarker application.properties配置文件 ###FreeMarker配置 spring.freemarker.template-loader ...
- Springboot模板(thymeleaf、freemarker模板)
目的: 1.thymeleaf模板 2.Freemarker模板 thymeleaf模板 thymeleaf 的优点: 支持html5标准,页面无须部署到servlet开发到服务器上,直接通过浏览器就 ...
随机推荐
- CF(438D) The Child and Sequence(线段树)
题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...
- 作为刚開始学习的人应该怎样来学习FPGA
FPGA作为一种高新的技术.已经逐渐普及到了各行各业.不管是消费类.通信类.电子行业都无处不在它的身影,从1985年第一颗FPGA诞生至 今,FPGA已经历了将近20多个年头,从当初的几百个门电路到如 ...
- 前台ajax验证技术采用正则表达式
var flag1,flag2,flag3,flag4,flag5,flag6=false; function val(obj) { var regBox = { /* regEmail : /^([ ...
- modSecurity规则学习(三)——SecRule
通用格式 SecRule VARIABLES OPERATOR [TRANSFORMATION_FUNCTIONS, ACTIONS] 阶段phase (1)request headers (2) ...
- Excel显示当前日期
https://zhidao.baidu.com/question/431460329693825764.html 直接选中单元格,在公示栏输入=now()
- js插件---jqGrid插件如何使用
js插件---jqGrid插件如何使用 一.总结 一句话总结:jqdrid还是依赖加js初始化的方式,很多时候插件的问题一般都是引入的css和js的问题,jqgrid里面遇到的问题就是下载包有一些js ...
- js --- 事件冒泡 事件捕获
先上结论: 他们是描述事件触发时序问题的术语.事件捕获指的是从document到触发事件的那个节点,即自上而下的去触发事件.相反的,事件冒泡是自下而上的去触发事件.绑定事件方法的第三个参数,就是控制事 ...
- HTTP服务器状态码定义
HTTP服务器状态代码定义 1.1 消息1xx(Informational 1xx) 该类状态代码用于表示临时回应.临时回应由状态行(Status-Line)及可选标题组成, 由空行终止.HTTP/1 ...
- #学习笔记#——JavaScript 数组部分编程(三)
3.在数组 arr 末尾添加元素 item.不要直接修改数组 arr,结果返回新的数组 主要考察数组的concat方法,代码如下: arr.concat(item); concat 方法不修改原数组. ...
- UVA 12075 Counting Triangles
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...