freeMarker入门使用

1.配置freemarker全局属性

public static Configuration getConfiguration(){
		if(configuration==null){
			Configuration temp = new Configuration();
			temp.setEncoding(Locale.getDefault(), "UTF-8");
			temp.setDefaultEncoding("UTF-8");
			temp.setDateFormat("yyyy-MM-dd");
			temp.setTimeFormat("HH:mm:ss");
			temp.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
			temp.setNumberFormat("0.####");
			configuration = temp;
		}
		return configuration;
	}

可以在java代码里配置freeMarker的配置文件,也可以在spring整合freeMarker时,在配置文件里配置。

2.生成页面

		Template template = null;
	
		String ftlFile = basePath + "/" + fileName + ".ftl";
		String htmlFile = basePath + "/" + fileName + ".html";
		
		try {
			template = cfg.getTemplate(ftlFile);
		} catch (FileNotFoundException e) {
			Logger.BIZ_LOG.error("模板文件[" + ftlFile + "]未找到,不生成文件");
			return;
		}
		template.setEncoding("UTF-8");
		//先生成html文件
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));
		//再根据ftl模板文件填充html
		template.process(rootMap, out);


这是根据ftl生成对应的html页面,在使用SpringMVC3时,当freeMarker作为模板时(从Web应用程序发送它返回给Web浏览器(mvc,view层)),看了源码暂时没看懂,慢慢来吧。
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值