在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. <p style="color:blue;"> ${message}</p>
  6. 祝:开心!
  7. </br>
  8. ${fromUserName}
  9. </br>
  10. ${time?date}
  11. </body>
  12. </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. @Test
  4. public void sendHtmlMailUsingFreeMarker() throws Exception {
  5. Map<String, Object> model = new HashMap<String, Object>();
  6. model.put("time", new Date());
  7. model.put("message", "这是测试的内容。。。");
  8. model.put("toUserName", "张三");
  9. model.put("fromUserName", "老许");
  10. Template t = configuration.getTemplate("welcome.ftl"); // freeMarker template
  11. String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
  12. logger.debug(content);
  13. //mailService.sendHtmlMail(to, "主题:html邮件", content);
  14. }
	@Autowired
Configuration configuration; //freeMarker configuration
@Test</br>
public void sendHtmlMailUsingFreeMarker() throws Exception {</br>
Map&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;();</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的模板:

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

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

</body>

</html>

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

springboot 使用FreeMarker模板(转)的更多相关文章

  1. SpringBoot获取Freemarker模板引擎,生成HTML代码

    今天用Ajax异步添加评论,加载Freemarker模板引擎,生成模板模块 1.新建Freemarker模板 <li id="${comment.oId}"> < ...

  2. SpringBoot使用freemarker模板

    导入依赖 <!-- 添加freemarker模版的依赖 --> <dependency> <groupId>org.springframework.boot< ...

  3. springboot整合freemarker模板引擎后在页面获取basePath绝对路径

    在项目中引用静态资源文件或者进行ajax请求时我们有时候会使用 ${basePath} ,其实这就是一种获取绝对路径的方式: 那么在springboot项目中要怎么配置才能使用 basePaht呢? ...

  4. SpringBoot整合freemarker模板

    一.目录展示 二.导入依赖 三.application.properties配置文件 四.在src/main/resource/templates文件夹中创建HelloFreeMarker.ftl文件 ...

  5. springboot之freemarker 和thymeleaf模板web开发

    Spring Boot 推荐使用Thymeleaf.FreeMarker.Velocity.Groovy.Mustache等模板引擎.不建议使用JSP. 一.Spring Boot 中使用Thymel ...

  6. 使用 FreeMarker模板 Springboot 发送邮件

    四.使用 FreeMarker模板 HTML 标签的字符串拼接是一件很棘手的事.因为在你的大脑中解析HTML标签并想象它在渲染时会是什么样子是挺困难的.而将HTML混合在Java代码中又会使得这个问题 ...

  7. SpringBoot集成freemarker和thymeleaf模板

    1.在MAVEN工程POM.XML中引入依赖架包 <!-- 引入 freemarker 模板依赖 --> <dependency> <groupId>org.spr ...

  8. SpringBoot基础及FreeMarker模板

    案例springboot_freemarker application.properties配置文件 ###FreeMarker配置 spring.freemarker.template-loader ...

  9. Springboot模板(thymeleaf、freemarker模板)

    目的: 1.thymeleaf模板 2.Freemarker模板 thymeleaf模板 thymeleaf 的优点: 支持html5标准,页面无须部署到servlet开发到服务器上,直接通过浏览器就 ...

随机推荐

  1. CF(438D) The Child and Sequence(线段树)

    题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...

  2. 作为刚開始学习的人应该怎样来学习FPGA

    FPGA作为一种高新的技术.已经逐渐普及到了各行各业.不管是消费类.通信类.电子行业都无处不在它的身影,从1985年第一颗FPGA诞生至 今,FPGA已经历了将近20多个年头,从当初的几百个门电路到如 ...

  3. 前台ajax验证技术采用正则表达式

    var flag1,flag2,flag3,flag4,flag5,flag6=false; function val(obj) { var regBox = { /* regEmail : /^([ ...

  4. modSecurity规则学习(三)——SecRule

    通用格式 SecRule VARIABLES OPERATOR [TRANSFORMATION_FUNCTIONS, ACTIONS]   阶段phase (1)request headers (2) ...

  5. Excel显示当前日期

    https://zhidao.baidu.com/question/431460329693825764.html 直接选中单元格,在公示栏输入=now()

  6. js插件---jqGrid插件如何使用

    js插件---jqGrid插件如何使用 一.总结 一句话总结:jqdrid还是依赖加js初始化的方式,很多时候插件的问题一般都是引入的css和js的问题,jqgrid里面遇到的问题就是下载包有一些js ...

  7. js --- 事件冒泡 事件捕获

    先上结论: 他们是描述事件触发时序问题的术语.事件捕获指的是从document到触发事件的那个节点,即自上而下的去触发事件.相反的,事件冒泡是自下而上的去触发事件.绑定事件方法的第三个参数,就是控制事 ...

  8. HTTP服务器状态码定义

    HTTP服务器状态代码定义 1.1 消息1xx(Informational 1xx) 该类状态代码用于表示临时回应.临时回应由状态行(Status-Line)及可选标题组成, 由空行终止.HTTP/1 ...

  9. #学习笔记#——JavaScript 数组部分编程(三)

    3.在数组 arr 末尾添加元素 item.不要直接修改数组 arr,结果返回新的数组 主要考察数组的concat方法,代码如下: arr.concat(item); concat 方法不修改原数组. ...

  10. UVA 12075 Counting Triangles

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...