FreeMarker

  • 介绍
FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。 官方网址:http://freemarker.org/ 这里有一个可以在线测试FreeMaker的网站:http://freemarker-online.kenshoo.com/。大家在学习的时候,可以试试。 另外,还有一个模板引擎Velocity,也是非常出名的。
  • 插件
FreeMaker在几个常用的IDE中都有语法高亮提示的插件, 具体怎么安装可以查考这里(http://freemarker.org/editors.html)。 多说一句,如果没有语法高亮功能的编辑器,基本上就不能用,所以插件还是很有必要的。
  • HelloWorld
下面是一段利用FreeMarker的API生成一个XML文档的例子(其中的User类您可以自己写一个,只要有name和age属性即可): [codesyntax lang="java"]
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

/**
 * 这是一个使用Freemarker生成XML文件的Helloworld。需要添加下面的的Maven依赖:
 * <pre>
 * {@code
 * <dependency>
 * 	<groupId>org.freemarker</groupId>
 * 	<artifactId>freemarker</artifactId>
 *	<version>2.3.22</version>
 * </dependency>
 * }
 * </pre>
 * 本程序执行完毕后会将输出文件直接打开。
 * @author suren
 * @date 2015年12月16日 上午8:12:27
 */
public class Test {

	/**
	 * TODO
	 * @param args
	 * @throws IOException 
	 * @throws ParseException 
	 * @throws MalformedTemplateNameException 
	 * @throws TemplateNotFoundException 
	 * @throws TemplateException 
	 */
	public static void main(String[] args) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		@SuppressWarnings("deprecation")
		Configuration conf = new Configuration();
		Template template = conf.getTemplate("target/classes/users.xml");
		
		File outFile = new File("d:/test.log");
		Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
		
		List<User> userList = new ArrayList<User>();
		userList.add(new User("name1", 12));
		userList.add(new User("name2", 13));
		userList.add(new User("name3", 14));
		
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("userList", userList);
		
		template.process(dataMap , out);
		
		out.flush();
		out.close();
		
		Desktop.getDesktop().open(outFile);
	}

}
[/codesyntax] 下面是模板文件: [codesyntax lang="xml"]
<?xml version="1.0" encoding="UTF-8"?>
<#list userList as user>
<user>
    <name value="${user.name}" />
    <age value="${user.age}" max=<#if user.age == 12>"true"<#else>"false"</#if> />
</user>
</#list>
[/codesyntax]
  • 变量
<#if parameters.runtimes??></#if> 这里是runtimes不为空的判断 <#if parameters.autostart!false></#if> 这里是当变量autostart为null或者不存在时给指定的默认值 ${options}.autostart = ${parameters.autostart?string("true", "false")}; 这里将autostart从布尔值变为字符串类型
  • 逻辑判断
<#if (!autostart?? || autostart!=true) && (!start?? || start!=true)></#if> 这是一个多层嵌套的逻辑判断,意思是autostart不存在或者值不为true,并且start不存储或者start的值不为true
  • 遍历list
[codesyntax lang="xml"]
<#list data as col>
${col_index}--${col}
</#list>
[/codesyntax]
  • 遍历map
[codesyntax lang="xml"]
<#list data?keys as key>
${key}---${data['${key}']}
</#list>

<#if (data['编号'])??>
<td width="${data['编号']}">${x_index+1}</td>
</#if>
[/codesyntax]
  • 宏 macro
这里的宏类似于函数的感念,可以重复调用。macro后的第一个字符串是宏名称(hanshuming),从第二个开始就是参数名了。在调用宏的时候,参数的顺序不做要求。 [codesyntax lang="xml"]
<#macro print count>
	<#if count gte 0>
${count}
	<#assign a=count-1>
	<@print count=a></@print>
	</#if>
</#macro>

<@print count=12></@print>
[/codesyntax] 在定义宏的时候,可以给参数添加默认值,有默认值的参数在调用的时候可以不赋值: <#macro print count other=12> 在宏定义里还可以使用嵌套,如下: [codesyntax lang="xml"]
<#macro print count other=12>
	<#if count gte 0>
${count}---${other}<#nested>
	<#assign a=count-1>
	<@print count=a><#nested></@print>
	</#if>
</#macro>

<@print count=12>hereis nested</@print>
[/codesyntax]
  • json
 
  • 使用手册
这里是官方的使用手册:http://freemarker.org/docs/index.html
  • 参考
FreeMaker百度百科 http://blog.sina.com.cn/s/blog_812a917c0100u0q8.html

转载于:https://my.oschina.net/surenpi/blog/816800

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值