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开发到服务器上,直接通过浏览器就 ...
随机推荐
- 实用的 Python 包 —— 使用 win32 的剪贴板
1. usage >> import win32clipboard >> win32clipboard.OpenClipboard() >> win32clipbo ...
- POJ 2181 贪心
思路: 贪心 对于每个波浪 ans+=最大值-最小值 注意最后一定是选最大值 //By SiriusRen #include <cstdio> using namespace std; i ...
- js函数的解析与执行过程
function f(a,b,c){ alert(a);//函数字符串 alert(b); var b = 5; function a(){ } } f(1,2); //预处理 lexicalEnvi ...
- JavaScript笔记(2)
-->变量的定义 1.取得并使用值是所有程序设计中的要点 2.JS中的变量是没有类型的,在JS中只有值才持有类型,变量所持有的是其对应值的类型. 3.变量的取名要符合标识符的规则: (1)一个J ...
- 【2017 Multi-University Training Contest - Team 2】TrickGCD
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6053 [Description] 给你一个b数组,让你求一个a数组: 要求,该数组的每一位都小于等 ...
- JMS基础知识
JMS规范: jms的基本构件: 连接工厂(connectionFactory):客户用来创建连接的对象.比如:activeMQ提供的ActiveMQConnectionFactory. 连接(co ...
- 利用淘宝ip库限制地区访问
https://sss.one/97.html 利用淘宝ip库限制地区访问 有些应用可能需要对某些地区的用户进行限制访问 在采用才此方法的时候,可以利用一些ip库对访问者的ip进行判断 淘宝ip库地址 ...
- php中如何动态获取函数的参数
php动态获取函数参数 一.总结 一句话总结:a.PHP 在用户自定义函数中支持可变数量的参数列表.其实很简单,只需使用 func_num_args() , func_get_arg() ,和 fun ...
- WdatePicker日期控件的使用
将压缩包中的文件连带文件夹添加到项目中去,注意要完整的添加到项目中去,不要更改了其目录结构 然后在aspx页面中直接使用即可: 首先引入: <script src="/Controls ...
- Day6下午题解1
预计分数:100+?+30=130+? 实际分数:100+25+30=155 T1 https://www.luogu.org/problem/show?pid=T15920 DP裸题,用dp[i][ ...