您的位置:首页 > 其它

freemaker简单演示

2015-12-13 22:02 316 查看
package com.whb.test;

import java.io.PrintWriter;

import java.util.HashMap;

import java.util.Map;

import org.junit.Test;

import freemarker.template.Configuration;

import freemarker.template.Template;

/**

 * @author WHB

 *

 */

public class TestFreeMarker {

@Test
public void testHello(){
//创建Configruation
Configuration cfg = new Configuration();

//System.out.println();
//new File("dd").mkdir();
//System.out.println(new File("ftl").getAbsolutePath());

//设置config中加载的模板路径
try {
cfg.setClassForTemplateLoading(TestFreeMarker.class, "/hello/ftl");
//获取模板文件,用于已经设置默认路径是/ftl
//此时hello.ftl就是ftl下的模板文件
Template temp = cfg.getTemplate("hello.ftl");
//创建数据文件,使用map来进行设置
Map<String,Object> root = new HashMap<String,Object>();
root.put("username", "校长");

//通过模板和文件输出相应的数据

temp.process(root, new PrintWriter(System.out));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  freemarker示例