freemarker 模板配制及使用

来源:互联网 发布:淘宝图片优化软件 编辑:程序博客网 时间:2024/06/01 09:32

模板文件

<html>
<title>
${title}
</title>
<body>
${msg}
</body>
</html>



使用模板生成文件


@Test
public void test1() throws IOException, TemplateException{

//创建模板对象

Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);

//配置模板位置

configuration.setDirectoryForTemplateLoading(new File("src/main/webapp/WEB-INF/templates"));

//获取模板

Template template = configuration.getTemplate("hello.ftl");

//配置数据对象

Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "呵呵呵");
map.put("msg", "哈哈哈");
template.process(map, new PrintWriter(System.out));--------------------输出打印流
}


控制台输出

<html>
<title>
呵呵呵
</title>
<body>
哈哈哈
</body>
</html>


process(Object dataModel, Writer out)方法  第一个参数传入模板,第二个参数传入输出流,生成文件用文件输出流