首先需要到freemarker官方下载freemarker的jar包,导入到WEB-INF/lib;
1、先建个freemarker的工具类,FreemarkerUtil.java
package util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreemarkerUtil {
public Template getTemplate(String name) {
try {
//通过Freemaker的Configuration读取相应的ftl
Configuration cfg = new Configuration();
//设定去哪里读取相应的ftl模板文件
cfg.setClassForTemplateLoading(this.getClass(),"/ftl");
//在模板文件目录中找到名称为name的文件
Template temp = cfg.getTemplate(name);
return temp;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 控制台输出
* @param name
* @param root
*/
public void print(String name,Map<String,Object> root) {
try {
//通过Template可以将模板文件输出到相应的流
Template temp = this.getTemplate(name);
temp.process(root, new PrintWriter(System.out));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 输出HTML文件
* @param name
* @param root
* @param outFile
*/
public void fprint(String name,Map<String,Object> root,String outFile) {
FileWriter out = null;
try {
//通过一个文件输出流,就可以写到相应的文件中,此处用的是绝对路径
out = new FileWriter(new File("D:\\MyEclipse\\MyEclipse8.6Project\\freemarker\\WebRoot\\WEB-INF\\page\\"+outFile));
Template temp = this.getTemplate(name);
temp.process(root, out);
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} finally {
try {
if(out!=null) out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2 、在src目录下建个ftl包,用于存放ftl模板文件,this.getClass() 就是根据当前类的路径获取模板文件位置
01.ftl
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
</head>
<body>
<h1>你好${username}</h1>
</body>
</html>
02.ftl
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>你好:${username}</h1>
</body>
</html>
03.ftl
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>${user.id}-----${user.name}-----${user.age}</h1>
<#if user.age lt 12>
${user.name}还是一个小孩
<#elseif user.age lt 18>
${user.name}快成年
<#else>
${user.name}已经成年
</#if>
</body>
</html>
04.ftl
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<#list users as user>
${user.id}---------${user.name}-------${user.age}<br/>
</#list>
</body>
</html>
05.ftl
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<#include "/inc/top.ftl"/>
<hr/>
<#list users as user>
${user.id}---------${user.name}-------${user.age}<br/>
</#list>
</body>
</html>
06.ftl
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${user.id}-------${user.name}------${user.group!} <#-- !后为空就不输出 -->
<#--${user.group.name!}--><#-- 按照以上的方式加! freemarker仅仅只会判断group.name是不是空值 -->
${(user.group.name)!"没有任何值存在"} <#-- 处理空值加上(),否则只会判断最后一个属性 -->
${(a.b)!"没有a.b元素"}
<#if (a.b)??> <#--if后不用加$-->
不为空
<#else>
为空
</#if>
</body>
</html>
3、再建个Junit的测试类 FreemarkerTest.java
package junit.test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.BeforeClass;
import org.junit.Test;
import bean.User;
import util.FreemarkerUtil;
public class FreemarkerTest {
private static FreemarkerUtil fu;
private static Map<String,Object> map = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
fu = new FreemarkerUtil();
map = new HashMap<String,Object>();
}
//1、创建简单freemarker
@Test
public void test01() {
//1、创建数据模型
Map<String,Object> root = new HashMap<String,Object>();
//2、为数据模型添加值
root.put("username", "张三");
//3、将数据模型和模板组合的数据输出到控制台
fu.print("01.ftl", root);
fu.fprint("02.ftl", root, "02.html");
}
//2.freemarker输出对象
@Test
public void test02() {
map.put("user", new User(1,"李四",16));
sprint("03.ftl");
fprint("03.ftl","03.html");
}
//4.输出数组
@Test
public void test04() {
List<User> users = Arrays.asList(new User(1,"张三",22),new User(2,"李四",33));
map.put("users",users);
sprint("04.ftl");
fprint("04.ftl","04.html");
}
//5、将包含页面静态化
@Test
public void test05() {
map.put("username", "管理员");
List<User> users = Arrays.asList(new User(1,"张三",22),new User(2,"李四",33));
map.put("users",users);
sprint("05.ftl");
fprint("05.ftl","05.html");
}
//6 freemarker 处理空值
@Test
public void test06() {
//此时user对象并没有group的值,这时如果在页面显示group直接报错
//freemarker不会处理空值
map.put("user",new User(1,"地点",22));
sprint("06.ftl");
}
private void sprint(String name) {
fu.print(name, map);
}
private void fprint(String name,String filename) {
fu.fprint(name, map, filename);
}
}
这样我们运行测试类就能生产html静态文件到WEB-INF/page文件下,我在工具类里用的是绝对路径,测试的时候要根据自己项目位置修改下
以上只是简单的实例,更多内容请参考freemarker的文档,官方有中文文档可以去看看。