|
Freemarker入门 1、Freemarker入门小demo 1.1 工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency> 1.2 创建模板文件 <html>
<head>
<meta charset="UTF-8">
<title>freemarker-demo</title>
</head>
<body>
<#-- 注释,不会有任何输出-->
<!-- 文本,直接输出 -->
${name},你好!${message}
</body>
</html> 模板中四种元素: A:文本,直接输出的部分 B:注释,即<#-- -->格式不会输出 C:插值(Interpolation):即${…}部分,将使用数据模型中的部分替代输出 D:FTL指令:FreeMarker指令,和HTML标记类似,名字前加#予以区分,不会输出 1.3 生成文件 //1、创建配置类
Configuration configuration = new Configuration(Configuration.getVersion());//2、设置模板所在的目录configuration.setDirectoryForTemplateLoading(new File("D:/IDEA-wordspace/freemarker/freemarkerdemo1/src/main/resources/"));//3、设置字符集configuration.setDefaultEncoding("utf-8");
//4、加载模型Template template = configuration.getTemplate("test.ftl");//5、创建数据模型Map map = new HashMap();map.put("name","张三");map.put("message","This is the first freemarker demo!");、//6、创建Writer对象FileWriter writer = new FileWriter(new File("D:\\index.html"));//7、输出
template.process(map, writer);//8、关闭writer对象writer.close();2、FTL指令 2.1 assign指令 (1)定义简单类型 <#assign username="张三">
用户名:${username} (2)定义对象类型 <#assign student={"name":"张三", "age":15}>
姓名:${student.name}年龄:${student.age} 2.2 inclue指令 创建模板文件head.ftl <h1>这是头部</h1> 在模板文件中使用Include指令引入head.ftl <#include "head.ftl"> 2.3 if指令 在模板文件加添加 <#if success=true>
您已通过实名验证
<#else>
您未通过实名认证
</#if> 在测试代码中进行赋值 map.put("success",true); 2.4 list指令 List goodsList=new ArrayList();
Map goods1=new HashMap();
goods1.put("name", "苹果");
goods1.put("price", 5.8);
Map goods2=new HashMap();
goods2.put("name", "香蕉");
goods2.put("price", 2.5);
Map goods3=new HashMap();
goods3.put("name", "橘子");
goods3.put("price", 3.2);
goodsList.add(goods1);
goodsList.add(goods2);
goodsList.add(goods3);
map.put("goodsList", goodsList); 在模板中进行遍历: <#list goodsList as goods>
${goods_index} ${goods.name}:${goods.price}<br>
</#list>3、内建函数 内建函数语法格式: 变量 + ? + 函数名称 3.1 获取集合大小 共${goodsList?size}条记录3.2 转换JSON字符串为对象 <#assign text="{'bank':'工商银行','account':'10101920201920212'}" />
<#assign data=text?eval>
开户行:${data.bank} 账号:${data.account} 3.3 日期格式化 测试代码中加入: map.put("today", new Date());当前日期:${today?date}<br>
当前时间:${today?time}<br>
当前日期+时间:${today?datetime}<br>
日期格式化:${today?string("yyyy-MM-dd")} 3.4 数字转化为字符串 测试代码中加入: map.put("point", 123456566);在模板中添加: 点数:${point} freemarker会自动将数字以每三位一个分隔符显示,当不需要分隔符时,需将数字转换为字符串,使用内建函数c 点数:${point?c} freemarker会自动将分隔符去掉 4、空值处理运算符 假如在模板中使用了变量,但在代码中没有对变量赋值,此时在运行生成时会抛出异常。 4.1 判断某变量是否存在:“??” 用法:variable??,如果变量存在,则返回true,否则返回false <#if aaa??>
aaa变量存在
<#else>
aaa变量不存在
</#if> 4.2 缺失变量默认值:“!” ${aaa!''} 5、算数符 5.1 算数运算符 FreeMarker表达式中完全支持算术运算,FreeMarker支持的算术运算符包括:+, - , * , / , % 5.2 逻辑运算符 逻辑运算符有如下几个: 逻辑与:&& 逻辑或:|| 逻辑非:! 逻辑运算符只能作用于布尔值,否则将产生错误 5.3 比较运算符 表达式中支持的比较运算符有如下几个: 1 =或者==:判断两个值是否相等. 2 !=:判断两个值是否不等. 3 >或者gt:判断左边值是否大于右边值 4 >=或者gte:判断左边值是否大于等于右边值 5 <或者lt:判断左边值是否小于右边值 6 <=或者lte:判断左边值是否小于等于右边值 注意: =和!=可以用于字符串,数值和日期来比较是否相等,但=和!=两边必须是相同类型的值,否则会产生错误,而且FreeMarker是精确比较,"x","x","X"是不等的.其它的运行符可以作用于数字和日期,但不能作用于字符串,大部分的时候,使用gt等字母运算符代替>会有更好的效果,因为 FreeMarker会把>解释成FTL标签的结束字符,当然,也可以使用括号来避免这种情况,如:<#if (x>y)>
|