package com.freemark;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreeMarkTest {
/**
* @param args
*/
private Configuration cfg;
private void init() throws Exception {
cfg = new Configuration(); //管理freemarker的类configuration
cfg.setDirectoryForTemplateLoading(new File("templates"));//设置该类的路径,templates为文件夹,它//下面有一个freemarker模板
}
private void process() throws IOException, TemplateException {
Map<String, Object> root = new HashMap<String, Object>();
StringWriter writer =new StringWriter();
//FreeMarkerUtil utl;
root.put("name", "FreeMarker!");
root.put("msg", "first FreeMarker demo");
List list=new ArrayList();
list.add("hello");
list.add("word");
root.put("list",list);//把list放入map中
root.put("cut",123456);
Template t = cfg.getTemplate("myfreemark.ftl");//加载模板myfreemark.ftl
//t.process(root, new OutputStreamWriter(System.out));
t.process(root, writer);//进行过程输出的
System.out.println(writer.toString());
}
public static void main(String[] args) throws Exception {
FreeMarkTest ft = new FreeMarkTest();
ft.init();
ft.process();
}
}
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreeMarkTest {
/**
* @param args
*/
private Configuration cfg;
private void init() throws Exception {
cfg = new Configuration(); //管理freemarker的类configuration
cfg.setDirectoryForTemplateLoading(new File("templates"));//设置该类的路径,templates为文件夹,它//下面有一个freemarker模板
}
private void process() throws IOException, TemplateException {
Map<String, Object> root = new HashMap<String, Object>();
StringWriter writer =new StringWriter();
//FreeMarkerUtil utl;
root.put("name", "FreeMarker!");
root.put("msg", "first FreeMarker demo");
List list=new ArrayList();
list.add("hello");
list.add("word");
root.put("list",list);//把list放入map中
root.put("cut",123456);
Template t = cfg.getTemplate("myfreemark.ftl");//加载模板myfreemark.ftl
//t.process(root, new OutputStreamWriter(System.out));
t.process(root, writer);//进行过程输出的
System.out.println(writer.toString());
}
public static void main(String[] args) throws Exception {
FreeMarkTest ft = new FreeMarkTest();
ft.init();
ft.process();
}
}