入门第一步:先添加freemarker所需的jar包,采取maven或者直接拷贝到工程的lib目录下都可以
入门第二步:编写相应的java代码
入门第三步:在.ftl文件中书取出相关的值
public class Test {
public static void main(String[] args) throws Exception {
Configuration cfg = new Configuration();//创建一个Configuration对象
cfg.setDirectoryForTemplateLoading(new File("templates")); //注意这里的File 内填写的是你的文件放置的路径
//创建数据模型
Map root = new HashMap();
root.put("user","老高");
//加载模板文件
Template t1 = cfg.getTemplate("a.ftl");
//显示生成的数据
Writer out = new OutputStreamWriter(System.out); //输出显示在控制台上
t1.process(root, out);
out.flush();
}
}
对应的a.ftl文件的操作:运用相关的对象
this is a simple freemarker test ${user}