Let`s drink code
This is the pojo class----user.java and Group.java
package com.jadyer.model;
public class User {
private int id;
private int age;
private String name;
private Group group;
/*==四个属性的setter和getter略==*/
public User() {}
public User(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
}
public class Group {
private int id;
private String name;
/*==二个属性的setter和getter略==*/
public Group() {}
public Group(int id, String name) {
this.id = id;
this.name = name;
}
}
This is FreeMarker Util Class----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();
}
}
}
This is our freemarker file----theNull.ftl
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<h2>${user.id}----${user.name}----${(user.group.name)!"group.name是空值"}</h2>
<#-- ??用于判断变量是否存在 -->
<#if (person.pname)??>
person.pname----存在该对象
<#else>
person.pname----不存在该对象
</#if>
<#--
FreeMarker中的空值处理
写法:${(user.group.name)!"空值"}
说明:1)括号会告诉FreeMarker去连续判断括号内的所有对象。否则${user.group.name!"空值"}则只会判断name对象
2)在括号后面加一个感叹号,就可以对里面的值进行判断了。若其值不存在,FreeMarker就会使用感叹号后面的值替之
-->
This is the test class----FreeMarkerTest.java
package com.jadyer.test;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
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>();
//FreeMarker不会自动处理空值:此时user对象并没有Group值,如果在ftl中输出${user.group},则直接编译报错
rootMap.put("user", new User(1, 22, "张小凡"));
new FreeMarkerUtil().print("theNull.ftl", "/ftl", rootMap);
new FreeMarkerUtil().printFile("theNull.ftl", "/ftl", rootMap, new File("D:\\ftl\\my.html"));
}7
}