目录结构
a.ftl
你好,${userName}今天精神不错啊!
---------------------------------------
<#if userName=="小明">是的精神不错</#if>
---------------------------------------
<#if random gt 18>
成年${random}
<#else>
未成年${random}
</#if>
---------------------------------------
<#if random gte 90>
优秀${random}
<#elseif random gte 80>
很优秀${random}
<#else>
还优秀${random}
</#if>
---------------------------------------
<#-- 遍历对象list -->
<#list country as guojia>
<b>${guojia.country},${guojia.area}</b><br/>
</#list>
---------------------------------------
<#--调用同根目录下文件的引用 -->
<#include "header/header.ftl"/><br/>
<#include "included.txt"/><br/>
---------------------------------------
<#--不带参数的宏 -->
<#macro ma>
<b>宏指令1第一行</b><br/>
<b>宏指令1第二行</b>
</#macro>
<@ma/><@ma/>
---------------------------------------
<#--带参数的宏 -->
<#macro mb a b c d>
${a}--${b}--${c}--${d}
</#macro>
<@mb a="xiao" b="ming" c="gun" d="dan"/>
<@mb "xiao" "zhang" "jin" "lai"/>
---------------------------------------
<#--链接带参数的宏<#nested> -->
<#macro mc>
<table>
<tr>
<td><#nested></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</#macro>
<@mc>
xiao mei
</@mc>
---------------------------------------
<#--定义变量 -->
<#assign mail = "15452@qq.com"/>
${mail}
---------------------------------------
<#--命名空间 -->
<#import "footer/footer.ftl" as bn/>
<@bn.copyright date = "2010-2018"/>
${bn.email}
<#assign email = "1602458963@qq。com"/>
${email}
<#assign email = "3@qq。com" in bn/>
${bn.email}
---------------------------------------
<#--定义变量 -->
<#assign a="ssss">
<#assign b=1>
<#assign c=true>
<#assign d=[1,2,3,4,5]>
<#assign e={"key1":"sss","key2":22}>
---------------------------------------
<#--遍历map方式1和取值 -->
<#if e?exists>
<#list e?keys as key>
<tr>
<td>${key}</td>
<td>${e[key]}</td>
</tr>
</#list>
</#if>
${e["key1"]}
<#assign keys = e?keys/>
<#list keys as key>
key:${key}-value:${e["${key}"]}
</#list>
<#list e?values as value>
${value}
</#list>
---------------------------------------
<#--获取时间 -->
${date?string("yyyy-MM-dd HH:mm:ss")}
---------------------------------------
<#--输出集合 -->
${d[0]}
${d[1]}
---------------------------------------
<#--内建函数 -->
${htm1?html}
${sss!"空的"}
<#if userName??>niiiiii</#if>
---------------------------------------
included.txt
我是test包含txt文件内容
header.ftl
我是头部
footer.ftl
<#macro copyright date>
<p>Copyright (c) ${date} 大森林农业发展中心</p>
</#macro>
<#assign email = "vjvjv@163.com">
out.ftl
我是模板目录之外的fddfdffddddd
Address.java
package com.dasenlin.test.freemaker;
public class Address {
private String country;
private String area;
public Address() {
super();
}
public Address(String country, String area) {
super();
this.country = country;
this.area = area;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
TestFreemaker.java
package com.dasenlin.test.freemaker;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
* 测试freemaker
* @author Toshiba
*
*/
public class TestFreemaker {
public static void main(String[] args) {
try {
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("templates"));
Map<Object, Object> root = new HashMap<Object, Object>();
root.put("userName", "小明");
root.put("random", new Random().nextInt(100));
List<Object> li = new ArrayList<Object>();
li.add(new Address("中国","北京"));
li.add(new Address("中国","香港"));
Address address=new Address();
address.setCountry("美国");
address.setArea("拉斯维加斯");
li.add(address);
root.put("country", li);
root.put("date",new Date());
root.put("htm1", "<b>粗体</b>");
Template ftl = cfg.getTemplate("a.ftl");
Writer out = new OutputStreamWriter(System.out);
ftl.process(root, out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}
}