筑基FreeMarker

完整版见https://jadyer.github.io/




这里用到的是FreeMarker-2.3.19


这是用到的实体类User.java

package com.jadyer.model;

public class User {
	private int id;
	private int age;
	private String name;
	/*==三个属性的setter和getter略==*/
	public User() {}
	public User(int id, int age, String name) {
		this.id = id;
		this.age = age;
		this.name = name;
	}
}

这是我们自己写的工具类FreeMarkerUtil.java

package com.jadyer.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Map;

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

public class FreeMarkerUtil {
	/**
	 * 获取指定目录下的模板文件
	 * @param name       模板文件的名称
	 * @param pathPrefix 模板文件的目录
	 */
	public Template getTemplate(String name, String pathPrefix) throws IOException{
		Configuration cfg = new Configuration(); //通过FreeMarker的Configuration对象可以读取ftl文件
		cfg.setClassForTemplateLoading(this.getClass(), pathPrefix); //设置模板文件的目录
		cfg.setDefaultEncoding("UTF-8");       //Set the default charset of the template files
		Template temp = cfg.getTemplate(name); //在模板文件目录中寻找名为"name"的模板文件
		return temp; //此时FreeMarker就会到类路径下的"pathPrefix"文件夹中寻找名为"name"的模板文件
	}
	
	/**
	 * 根据模板文件输出内容到控制台
	 * @param name       模板文件的名称
	 * @param pathPrefix 模板文件的目录
	 * @param rootMap    模板的数据模型
	 */
	public void print(String name, String pathPrefix, Map<String,Object> rootMap) throws TemplateException, IOException{
		this.getTemplate(name, pathPrefix).process(rootMap, new PrintWriter(System.out));
	}
	
	/**
	 * 根据模板文件输出内容到指定的文件中
	 * @param name       模板文件的名称
	 * @param pathPrefix 模板文件的目录
	 * @param rootMap    模板的数据模型
	 * @param file       内容的输出文件
	 */
	public void printFile(String name, String pathPrefix, Map<String,Object> rootMap, File file) throws TemplateException, IOException{
		Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
		this.getTemplate(name, pathPrefix).process(rootMap, out); //将模板文件内容以UTF-8编码输出到相应的流中
		if(null != out){
			out.close();
		}
	}
}

这是我们自己编写的位于//src//ftl//包中的FreeMarker模板文件theBase.ftl

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<#-- 在FreeMarker中可以通过这种方式,获取到数据模型中的值 -->
<h1>My Blog is ${myblog}</h1>

<h2>${user.id}----${user.age}----${user.name}</h2>

<#if user.age == 50>
	${user.name} is 此人50岁了
<#elseif user.age gt 28>
	${user.name} is 此人大约28到50岁之间
<#elseif user.age lt 22>
	${user.name} is 此人还不到22岁
<#else>
	${user.name} is 此人大约22到28岁之间
</#if>

<#list userList as user>
	${user.id}----${user.age}----${user.name}<br/>
</#list>

最后是使用JUnit4.x编写的一个测试类

package com.jadyer.test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.jadyer.model.User;
import com.jadyer.util.FreeMarkerUtil;

import freemarker.template.TemplateException;

public class FreeMarkerTest {
	@Test
	public void print() throws TemplateException, IOException{
		Map<String,Object> rootMap = new HashMap<String,Object>();
		List<User> userList = Arrays.asList(new User(1,36,"陈文锦"), new User(2,32,"齐羽"));
		rootMap.put("userList", userList);
		rootMap.put("user", new User(1,50,"吴三省"));
		rootMap.put("myblog", "http://blog.csdn.net/jadyer");       //创建数据模型,并为数据模型添加值
		new FreeMarkerUtil().print("theBase.ftl", "/ftl", rootMap); //将数据模型和模板组合的数据输出到流
		new FreeMarkerUtil().printFile("theBase.ftl", "/ftl", rootMap, new File("D:\\ftl\\my.html"));
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值