目录
FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。
2.在pom文件中引入依赖
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
3.模板语法
访问map中的key
${key}
访问pojo中的属性
${key.property}
学生信息:<br>
学号:${student.id}<br>
姓名:${student.name}<br>
年龄:${student.age}<br>
住址:${student.address}<br>
取集合中的元素
<#list studentList as student>
${student.id}/${studnet.name}
</#list>
取循环中的下标
<#list studentList as student>
${student_index}
</#list>
判断
<#if stu_index%2 == 0>
<tr bgcolor="red">
<#else>
<tr bgcolor="green">
</#if>
日期类型格式化
NULL值处理
null值: ${val!"默认值"}<br>
判断val值是否为null:<br>
<#if val??>
val有值为:${val}
<#else>
val为null
</#if><br>
include标签
<#include "hello.ftl">
4.测试类
test类
package lx.test.freemarker;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class TestFreeMarker {
@Test
public void genFile() throws Exception {
// 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。
Configuration configuration = new Configuration(Configuration.getVersion());
// 第二步:设置模板文件所在的路径。
configuration.setDirectoryForTemplateLoading(new File("D:/mars.2_workspace/e3-item-web/src/main/webapp/WEB-INF/ftl"));
// 第三步:设置模板文件使用的字符集。一般就是utf-8.
configuration.setDefaultEncoding("utf-8");
// 第四步:加载一个模板,创建一个模板对象。
Template template = configuration.getTemplate("hello.ftl");
// 第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
Map dataModel = new HashMap<>();
//向数据集中添加数据
dataModel.put("hello", "this is my first freemarker test.");
Student student = new Student(1,"xiaoming",18,"lx");
dataModel.put("student", student);
List<Student> stuList = new ArrayList<>();
stuList.add(new Student(1, "a", 1, "lx1"));
stuList.add(new Student(2, "b", 2, "lx2"));
stuList.add(new Student(3, "c", 3, "lx3"));
stuList.add(new Student(4, "d", 4, "lx4"));
stuList.add(new Student(5, "e", 5, "lx5"));
dataModel.put("stuList", stuList);
dataModel.put("date", new Date());
dataModel.put("val", 1);
// 第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。
//Writer out = new FileWriter(new File("D:/temp/freemarker2/hello.html"));
Writer out = new FileWriter(new File("D:/temp/freemarker2/student.html"));
// 第七步:调用模板对象的process方法输出文件。
template.process(dataModel, out);
// 第八步:关闭流。
out.close();
}
}
hello.ftl
${hello}
student.ftl
<html>
<head>
<title>student</title>
</head>
<body>
学生信息:<br>
学号:${student.id}<br>
姓名:${student.name}<br>
年龄:${student.age}<br>
住址:${student.address}<br>
<table border="1">
<tr>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>住址</th>
</tr>
<#list stuList as stu>
<#if stu_index%2 == 0>
<tr bgcolor="red">
<#else>
<tr bgcolor="green">
</#if>
<td>${stu_index}</td>
<td>${stu.id}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.address}</td>
</tr>
</#list>
</table>
<!--可以使用?date,?time,?datetime,?string("partten")--!>
当前日期: ${date?string("yyyy/MM/dd HH:mm:ss")}<br>
null值: ${val!"默认值"}<br>
判断val值是否为null:<br>
<#if val??>
val有值为:${val}
<#else>
val为null
</#if><br>
引用模板测试:<br>
<#include "hello.ftl">
</body>
</html>