package com.freemarker;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class TestFreeMarker {
private Configuration cfg;
public Configuration getCfg() {
return cfg;
}
public void init()
throws Exception {
cfg =
new Configuration();
cfg.setDirectoryForTemplateLoading(
new File("bin/com/freemarker"));
}
/**
* @param args
* @throws Exception
*/ public static void main(String[] args)
throws Exception {
TestFreeMarker obj =
new TestFreeMarker();
obj.init();
//
List<HashMap<String, Object>> list =
new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> dataMap =
new HashMap<String, Object>();
dataMap.put("id", "1");
dataMap.put("name", "张三");
dataMap.put("score", 95);
list.add(dataMap);
dataMap =
new HashMap<String, Object>();
dataMap.put("id", "2");
dataMap.put("name", "李四");
dataMap.put("score", 88);
list.add(dataMap);
dataMap =
new HashMap<String, Object>();
dataMap.put("id", "3");
dataMap.put("name", "王五");
dataMap.put("score", 72);
list.add(dataMap);
Entity est =
new Entity();
est.setId(99);
est.setName("dingzh@zbiti.com");
//
Map<String, Object> root =
new HashMap<String, Object>();
root.put("listDatas", list);
root.put("est", est);
root.put("message", "My first test freemarker...");
//
Template t = obj.getCfg().getTemplate("TestFreeMarker.ftl");
Writer out =
new OutputStreamWriter(
new FileOutputStream("TestFreeMarker.html"), "GBK");
t.process(root, out);
out.flush();
out.close();
System.out.println("Successfull................");
}
}

View Code Entity
package com.freemarker;
public class Entity {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}