FreeMarker HelloWord列子

来源:互联网 发布:mysql employees 导入 编辑:程序博客网 时间:2024/06/05 00:00

工程目录结构:

 

 

 

源码列子:

 

package org.ftl.test;import Java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.HashMap;import java.util.Map;import org.junit.Test;import freemarker.core.ParseException;import freemarker.template.Configuration;import freemarker.template.MalformedTemplateNameException;import freemarker.template.Template;import freemarker.template.TemplateException;import freemarker.template.TemplateNotFoundException;import freemarker.template.Version;public class TestFreemarker {  @Test public void testHelloToHtml() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException{  //1:创建configuration  Configuration cfg= new Configuration(new Version("2.3.23"));  //2:设置configuration中模版加载的路径  //基于classpath加载路径,并且所有模版都放在/ftl/templates中  cfg.setClassForTemplateLoading(TestFreemarker.class, "/ftl/templates");  //3:获取模版文件  Template template=cfg.getTemplate("hello.ftl");  //4:创建数据,一般使用map  Map<String, String> root= new HashMap<String, String>();  root.put("username", "jacky");  //5:通过数据与模版的结合进行输出  template.process(root, new FileWriter(new File("d:/test/freemarker/hello.html")));   }  //@Test public void testHello() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException{  //1:创建configuration  Configuration cfg= new Configuration(new Version("2.3.23"));  //2:设置configuration中模版加载的路径  //基于classpath加载路径,并且所有模版都放在/ftl/templates中  cfg.setClassForTemplateLoading(TestFreemarker.class, "/ftl/templates");  //3:获取模版文件  Template template=cfg.getTemplate("hello.ftl");  //4:创建数据,一般使用map  Map<String, String> root= new HashMap<String, String>();  root.put("username", "刘德华");  //5:通过数据与模版的结合进行输出  template.process(root, new PrintWriter(System.out));   }}

 

 


 

0 0
原创粉丝点击