首先在項(xiàng)目中建立存放模板的文件夾templates,然后放入模板文件person.ftl,內(nèi)容如下: Hello, my name is ${name}. I come from ${address}. Nice to meet you!
publicclass Test
{publicstaticvoid main(String[] args) throws IOException, TemplateException{Version version = new Version(2, 3, 1);Configuration cfg = new Configuration(version);cfg.setDirectoryForTemplateLoading(new File("templates"));cfg.setObjectWrapper(new DefaultObjectWrapper(version));Template temp = cfg.getTemplate("person.ftl");Map root = new HashMap();root.put("name", "張三");root.put("address", "中國-北京");Writer out = new OutputStreamWriter(System.out);temp.process(root, out);out.flush();}
}
輸出結(jié)果為:
Hello, my name is 張三. I come from 中國-北京. Nice to meet you!