FreeMarker基本操作(二)

package com.ninemax.test;

import java.io.File;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreeMarkerByTwo {
	@SuppressWarnings("all")
	public static void main(String[] args) throws Exception {
		// 创建 freemarker配置实例
		Configuration cfg = new Configuration();
		cfg.setDirectoryForTemplateLoading(new File("D:\\freemarker\\src"));
		// 创建数据类型
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", "other");
		map.put("num1", 2);
		map.put("num2", 3);
		// 加载模板文件
		Template t = cfg.getTemplate("macro.ftl");
		//显示生成后的数据
		t.process(map, new OutputStreamWriter(System.out));
	}

}

marco.ftl文件:

----------------------宏指令的使用 m1可以看成是方法的名称,num1、num2为入参----------------------
<#macro m1 num1 num2>
<#assign result=num1+num2>
<h3>${result}</h3>
</#macro>
<@m1 5 6/>
----------------------宏指令(嵌入式)-----------------
<#macro m2>
<h3><#nested></h3>
</#macro>
<@m2>hello world</@m2>

输出:

----------------------宏指令的使用 m1可以看成是方法的名称,num1、num2为入参----------------------
<h3>11</h3>
----------------------宏指令(嵌入式)-----------------
<h3>hello world</h3>


转载于:https://my.oschina.net/Tsher2015/blog/644374

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值