首先在我们的MyEclipse下创建一个普通Java Project,然后在工程下建个文件夹叫做templates, 在templates下建立模板文件叫作testFreeMarker.ftl,然后不要忘记加入freemarker.jar代码如下: welcome ${customer} to sunyang!
创建java文件 package com.test.freemarker; import java.io.File; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.HashMap; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class TestFreeMarker { private Configuration configuration; public void init() { configuration = new Configuration(); try { configuration.setDirectoryForTemplateLoading(new File("templates")); } catch (IOException e) { e.printStackTrace(); } } public void process() { Map<String, Object> data = new HashMap<String, Object>(); data.put("customer", "Jone"); Template template; try { template = configuration.getTemplate("testFreeMarker.ftl"); template.process(data, new OutputStreamWriter(System.out)); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { TestFreeMarker tfm = new TestFreeMarker(); tfm.init(); tfm.process(); } }