FreeMarker 简介(摘取:http://freemarker.foofun.cn/):
FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
模板编写为FreeMarker Template Language (FTL)。它是简单的,专用的语言, 不是像PHP那样成熟的编程语言。 那就意味着要准备数据在真实编程语言中来显示,比如数据库查询和业务运算, 之后模板显示已经准备好的数据。在模板中,你可以专注于如何展现数据, 而在模板之外可以专注于要展示什么数据。
这种方式通常被称为 MVC (模型 视图 控制器) 模式,对于动态网页来说,是一种特别流行的模式。 它帮助从开发人员(Java 程序员)中分离出网页设计师(HTML设计师)。设计师无需面对模板中的复杂逻辑, 在没有程序员来修改或重新编译代码时,也可以修改页面的样式。
而FreeMarker最初的设计,是被用来在MVC模式的Web开发框架中生成HTML页面的,它没有被绑定到 Servlet或HTML或任意Web相关的东西上。它也可以用于非Web应用环境中。
FreeMarker 是 免费的, 基于Apache许可证2.0版本发布。
Freemarker应用:
使用freemarker生成固定文本格式的word文档:
首先找到需要编辑的word文档,在word中将需要填写的部分(换句话说就是需要系统提供数据的部分)标记出来,如图所示(百度没有免费的文档供参考,全是手敲,凌乱的地方纯属瞎敲),标记部分的名称一定要住,方便后面修改,最好有固定的格式。
然后将word模板另存为xml文件(为了兼容,最好使用Word 2003 XML文档)
接下来替换标记。使用xml编辑工具(我用的NotePad++)打开该xml文件 (注意:xml文件样式可能比较乱,为方便查看,可以使用xml在线格式化工具格式化。),将标记的部分改为${tagName},如上文的nameA改为${nameA}。(注意:有些标记不是简单替换就可以的,需要进行非空判断等操作,具体的需要去了解framemarker中标签的使用。)如图:
接下来更改文件后缀为.ftl,注意:不要再使用word打开ftl文件;上面步骤是制作word转ftl模板的过程;接下来写个简单的servlet,让上面的努力实现一下;
首先创建一个工程freemarker,编写一个servlet,记录一下目录结构:
Normal.java是我创建的servlet
package com.no17.word;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class Normal extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//数据集
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("nameA", "房东");
dataMap.put("idcardA", "1234567890");
dataMap.put("nameB", "房客");
dataMap.put("idcardB", "0987654321");
dataMap.put("location", "中国");
dataMap.put("s1", "2018");
dataMap.put("s2", "02");
dataMap.put("s3", "01");
dataMap.put("e1", "2019");
dataMap.put("e2", "01");
dataMap.put("e3", "31");
dataMap.put("n", "12");
dataMap.put("rmb", "壹万元整");
dataMap.put("alb", "10000.00");
dataMap.put("y", "1");
dataMap.put("m", "4");
dataMap.put("telA", "13133332222");
dataMap.put("telB", "13122223333");
dataMap.put("qa1", "2018");
dataMap.put("qa2", "01");
dataMap.put("qa3", "28");
dataMap.put("qb1", "2018");
dataMap.put("qb2", "01");
dataMap.put("qb3", "29");
// 生成word并下载
File file = null;
WordExportUtils workUtil = new WordExportUtils();
InputStream inputStream = null;
ServletOutputStream out = null;
String fileName = "房屋租赁合同.doc";
String ftlName = "houserent.ftl";
try {
file = workUtil.wordCreate(dataMap, fileName, ftlName);
inputStream = new FileInputStream(file);
res.setCharacterEncoding("UTF-8");
res.setContentType("application/msword");
res.addHeader("Content-Disposition", "attachment;filename = wordexport.doc");
out = res.getOutputStream();
byte[] buffer = new byte[1024]; // 缓冲区
int bytesToRead = -1;
// 通过循环将读入的Word文件的内容输出到浏览器中
while ((bytesToRead = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null)
inputStream.close();
if (out != null)
out.close();
if (file != null)
file.delete();
}
}
}
WordExportUtils.java工具类;主要目的是自定义一些方式方法;
package com.no17.word;
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.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class WordExportUtils {
private Configuration config = null;
public WordExportUtils() {
config = new Configuration();
config.setDefaultEncoding("utf-8");
}
public File wordCreate(Map<String, Object> dataMap, String fileName, String ftlName) {
//设定包加载的路径
config.setClassForTemplateLoading(WordExportUtils.class, "/com/no17/word/template");
File outFile = new File(fileName);
//创建模板实例对象
Template template = null;
try {
template = config.getTemplate(ftlName);
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
template.process(dataMap, writer);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
return outFile;
}
}
最后new一个index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>framemarker导出word</title>
</head>
<body>
<form action="wordExport" method="post" name="form1">
<center>
<h1>framemarker导出word</h1>
<input type="submit" value="导出word">
</center>
</form>
</body>
</html>
启动项目,访问http://localhost:8080/freemarker/;页面及生成结果如下:
页面:
word文档
此处附带一下freemarker-2.3.19.jar的下载地址:http://www.java2s.com/Code/Jar/f/Downloadfreemarker2319jar.htm