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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值