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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章