freeMarker_01

FreeMarker 入门实例

public class FreeMarkerTest extends TestCase {
	
	
	public void test01() throws IOException, TemplateException{
		
		String dir = "E:/Workspaces/freeMarkerTest/src/com/ysen";
		
		
		Configuration cfg = new Configuration();
		//1从什么地方加载freemarker模板文件
		cfg.setDirectoryForTemplateLoading(new File(dir));
		//2加载模板
		Template temp = cfg.getTemplate("test01.ftl");
		
		//3定义数据
		Map root = new HashMap();
		root.put("helloWorld", "哈喽世界");
		//4定义输出
		Writer out = new FileWriter(dir+"/test01_out.txt");
		
		
		temp.process(root, out);
		 
		
	}
	
	
	//关于空值的处理问题
	public void test02() throws Exception{
		
		String dir ="E:/Workspaces/freeMarkerTest/src/com/ysen";
		
		Configuration cfg = new Configuration();
		
		cfg.setDirectoryForTemplateLoading(new File(dir));
		
		Template template = cfg.getTemplate("/test02.ftl");
		
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
		
		Map root = new HashMap();
		
		root.put("today", new Date());
		
		Writer out = new FileWriter(dir+"/test02.txt");
		
		template.process(root, out);
	
	}

	
	//关于集合的处理
	public void test03() throws Exception{
		
		String dir = "E:/Workspaces/freeMarkerTest/src/com/ysen";
		
		Configuration cfg = new Configuration();
		
		cfg.setDirectoryForTemplateLoading(new File(dir));
		Template template =  cfg.getTemplate("test03.ftl");
		
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
		
		Map root = new HashMap();
		List list = new ArrayList();
		for(int i=0; i<10; i++){
			list.add("listvalue"+i);
		}
		root.put("list", list);
		
		//定义输出
		Writer out = new FileWriter(dir+"/test03out.txt");
		
		template.process(root, out);
		
		
	}
	
	
	//关于freemarker的宏定义
	public void test04() throws Exception{
		
		String dir = "E:/Workspaces/freeMarkerTest/src/com/ysen";
		
		Configuration cfg = new Configuration();
		
		//配置freemarker从什么地方加载模板文件
		cfg.setDirectoryForTemplateLoading(new File(dir));
		
		//忽略异常
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
		
		//加载模板
		Template template = cfg.getTemplate("test04.ftl");
		
		//定义数据
		Map root = new HashMap();
		root.put("name", "李四");
		
		//定义输出
		Writer out = new FileWriter(dir+"/test04_out.txt");
		
		template.process(root, out);
	}
	
	//关于auto-import特性
	public void test05() throws Exception{
		
		String dir = "E:/Workspaces/freeMarkerTest/src/com/ysen";
		
		Configuration cfg = new Configuration();
		
		//配置freemarker从什么地方加载模板文件
		cfg.setDirectoryForTemplateLoading(new File(dir));
		
		//忽略异常
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
		
		//添加auto-import
		cfg.addAutoImport("my", "common.ftl");
		
		//加载模板
		Template template = cfg.getTemplate("test05.ftl");
		
		//定义数据
		Map root = new HashMap();
		root.put("name", "李四");
		
		//定义输出
		Writer out = new FileWriter(dir+"/test05_out.txt");
		
		template.process(root, out);
	}
}

 

 

test01.ftl

 

第一个测试程序:${helloWorld}

 

 

test02.ftl

第一个测试程序:<#if strvalue?exists>${strvalue}</#if>


${boolvalue?string("是","否")}

${today?string("yyyy年MM月dd日")}

 test03.ftl

 

<#list list as v>

	${v}-${v_index} [${v_has_next?string("y","n")} ]

</#list> 

 

test04.ftl

<#macro greet p>
	Hello,${p}
</#macro>

<@greet p="张三" />
<@greet p="${name}" />

 

test05.ftl

<@my.greet p="张三" />
<@my.greet p="${name}" />

 common.ftl

<#macro greet p>
	Hello,${p}
</#macro>

 

你可能感兴趣的:(freemarker)