要使用freemarker模板 要添加freemarker的jar包 下载地址 http://www.downza.cn/soft/215513.html
模板的结构是一个树结构
在eclipse创建一个java类
public class TestFreemarker {
public static void main(String[] args) throws IOException, TemplateException {
Configuration cfg = new Configuration();
//配置ftl查找目录
cfg.setDirectoryForTemplateLoading(new File("templates"));
//配置数据的抓取模式
cfg.setObjectWrapper(new DefaultObjectWrapper());
//构建数据
Map root = new HashMap();
root.put("user", "Big Joe");
Map latest = new HashMap();
root.put("latestProduct", latest);
latest.put("url", "products/greenmouse.html");
latest.put("name", "green mouse");
//实例化模板对象
Template temp=cfg.getTemplate("helloword.ftl");
//输出控制台
Writer out=new OutputStreamWriter(System.out);
temp.process(root, out);
out.flush();
out.close();
//生成html文件
/* //实例化模板对象
Template temp=cfg.getTemplate("helloword.ftl");
FileOutputStream for=new FileOutputStream("D:\\");
//生成html
Writer out=new OutputStreamWriter(fos);
temp.process(root, out);
out.flush();
out.close();*/
}
}
创建一个模板
这只是一个简单的freemarker