public void test1() throws IOException, TemplateException {
//1.模板
//(1)创建freemarker对象---创建配置对象,并指定freemarker的版本
Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
//(2)找到指定的模板文件---指定模板文件从何处加载的数据源,这里设置文件目录位置。
cfg.setDirectoryForTemplateLoading(new File("templates"));
//2.提供数据
Map root = new HashMap();
root.put("username", "maomaocong");
//3.获取模板文件
Template temp = cfg.getTemplate("test.ftl");
//4.设置输出为新的文件
Writer out = new OutputStreamWriter(new FileOutputStream("test.html"));
//5.将数据与新文件整合到一起,执行输出(将数据放到文件之中)
temp.process(root, out);
//6.刷新,关闭资源
out.flush();
out.close();
}
freemarker 底层
最新推荐文章于 2024-07-19 17:37:28 发布