FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写,
模板用servlet提供的数据动态地生成 HTML,模板语言是强大的直观的,编译器速度快,
输出接近静态HTML页面的速度。
一.Freemarker模板应用事例。
1.创建模板文件,在/resource/template目录下建立freemarkerLocal.ftl文件。
- ------------------普通变量获取-------------------
- ${user}
- ---------获取Map中属性--------------
- ${latestProduct.url}
- ${latestProduct.name}
- -------自定义的indexOf方法变量-------
- <#assign x = "something">
- ${indexOf("met", x)}
- ${indexOf("foo", x)}
- ------自定义的转换器变量(转换小写为大写,以标签形式展现)----------------
- blah1
- <@upperCase>
- blah2
- blah3
- @upperCase>
- blah4
- ---------------共享变量的获取,在Configuration中设置---------------
- <@to_upper>
- ${company}
- @to_upper>
[java] view plain copy print ?
- "FONT-SIZE: 18px">------------------普通变量获取-------------------
- ${user}
- ---------获取Map中属性--------------
- ${latestProduct.url}
- ${latestProduct.name}
- -------自定义的indexOf方法变量-------
- <#assign x = "something">
- ${indexOf("met", x)}
- ${indexOf("foo", x)}
- ------自定义的转换器变量(转换小写为大写,以标签形式展现)----------------
- blah1
- <@upperCase>
- blah2
- blah3
- @upperCase>
- blah4
- ---------------共享变量的获取,在Configuration中设置---------------
- <@to_upper>
- ${company}
- @to_upper>
------------------普通变量获取-------------------
${user}
---------获取Map中属性--------------
${latestProduct.url}
${latestProduct.name}
-------自定义的indexOf方法变量-------
<#assign x = "something">
${indexOf("met", x)}
${indexOf("foo", x)}
------自定义的转换器变量(转换小写为大写,以标签形式展现)----------------
blah1
<@upperCase>
blah2
blah3
@upperCase>
blah4
---------------共享变量的获取,在Configuration中设置---------------
<@to_upper>
${company}
@to_upper>
2.代码解析模板文件输出。
- public class FreemarkerLocalTest {
-
- public static void main(String[] args) throws Exception {
-
-
- Configuration cfg = new Configuration();
-
-
- cfg.setDirectoryForTemplateLoading(
- new File("D:/myspace/freemarker/resource/template"));
-
-
-
-
- cfg.setSharedVariable("to_upper", new UpperCaseTransform());
- cfg.setSharedVariable("company","FooInc.");
-
-
- cfg.setObjectWrapper(new DefaultObjectWrapper());
-
-
-
-
-
-
-
-
-
-
-
-
- Template temp = cfg.getTemplate("freemarkerLocal.ftl");
-
-
- Map root = new HashMap();
- root.put("user", "Big Joe");
- Map latest = new HashMap();
- root.put("latestProduct", latest);
- latest.put("url", "products/greenmouse.html");
- latest.put("name", "green mouse");
-
-
- root.put("indexOf", new IndexOfMethod());
-
-
- root.put("upperCase", new UpperCaseTransform());
-
-
- Writer out = new OutputStreamWriter(System.out);
- temp.process(root, out);
- out.flush();
- }
- }
[java] view plain copy print ?
- "FONT-SIZE: 18px">public class FreemarkerLocalTest {
-
- public static void main(String[] args) throws Exception {
-
-
- Configuration cfg = new Configuration();
-
-
- cfg.setDirectoryForTemplateLoading(
- new File("D:/myspace/freemarker/resource/template"));
-
-
-
-
- cfg.setSharedVariable("to_upper", new UpperCaseTransform());
- cfg.setSharedVariable("company","FooInc.");
-
-
- cfg.setObjectWrapper(new DefaultObjectWrapper());
-
-
-
-
-
-
-
-
-
-
-
-
- Template temp = cfg.getTemplate("freemarkerLocal.ftl");
-
-
- Map root = new HashMap();
- root.put("user", "Big Joe");
- Map latest = new HashMap();
- root.put("latestProduct", latest);
- latest.put("url", "products/greenmouse.html");
- latest.put("name", "green mouse");
-
-
- root.put("indexOf", new IndexOfMethod());
-
-
- root.put("upperCase", new UpperCaseTransform());
-
-
- Writer out = new OutputStreamWriter(System.out);
- temp.process(root, out);
- out.flush();
- }
- }
-
public class FreemarkerLocalTest {
public static void main(String[] args) throws Exception {
/* 一般在应用的整个生命周期中你仅需要执行一下代码一次*/
/* 创建一个合适的configuration */
Configuration cfg = new Configuration();
// 设置模板加载的方式
cfg.setDirectoryForTemplateLoading(
new File("D:/myspace/freemarker/resource/template"));
// 方式二(从Web上下文获取)
// void setServletContextForTemplateLoading(Object servletContext, String path);
// 设置模板共享变量,所有的模板都可以访问设置的共享变量
cfg.setSharedVariable("to_upper", new UpperCaseTransform());
cfg.setSharedVariable("company","FooInc.");
// 指定模板如何查看数据模型
cfg.setObjectWrapper(new DefaultObjectWrapper());
// 如果从多个位置加载模板,可采用以下方式
/**
FileTemplateLoader ftl1 = new FileTemplateLoader(new File("/tmp/templates"));
FileTemplateLoader ftl2 = new FileTemplateLoader(new File("/usr/data/templates"));
ClassTemplateLoader ctl = new ClassTemplateLoader(getClass(),"");
TemplateLoader[] loaders = new TemplateLoader[] { ftl1, ftl2,ctl };
MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
cfg.setTemplateLoader(mtl);**/
/* 而以下代码你通常会在一个应用生命周期中执行多次*/
/*获取或创建一个模版*/
Template temp = cfg.getTemplate("freemarkerLocal.ftl");
/*创建一个数据模型Create a data model */
Map root = new HashMap();
root.put("user", "Big Joe");
Map latest = new HashMap();
root.put("latestProduct", latest);
latest.put("url", "products/greenmouse.html");
latest.put("name", "green mouse");
// 方法变量,indexOf为自己定义的方法变量
root.put("indexOf", new IndexOfMethod());
// 转换器变量
root.put("upperCase", new UpperCaseTransform());
/* 合并数据模型和模版*/
Writer out = new OutputStreamWriter(System.out);
temp.process(root, out);
out.flush();
}
}
3.自定义的方法变量类IndexOfMethod
- public class IndexOfMethod implements TemplateMethodModel {
-
- public Object exec(List args) throws TemplateModelException {
- if (args.size() != 2) {
- throw new TemplateModelException("Wrong arguments");
- }
-
- return new SimpleNumber(((String) args.get(1))
- .indexOf((String) args.get(0)));
- }
- }
[java] view plain copy print ?
- "FONT-SIZE: 18px">public class IndexOfMethod implements TemplateMethodModel {
-
- public Object exec(List args) throws TemplateModelException {
- if (args.size() != 2) {
- throw new TemplateModelException("Wrong arguments");
- }
-
- return new SimpleNumber(((String) args.get(1))
- .indexOf((String) args.get(0)));
- }
- }
public class IndexOfMethod implements TemplateMethodModel {
public Object exec(List args) throws TemplateModelException {
if (args.size() != 2) {
throw new TemplateModelException("Wrong arguments");
}
// 返回第一个字符串首次出现在第二个字符串的位置
return new SimpleNumber(((String) args.get(1))
.indexOf((String) args.get(0)));
}
}
4.自定义的转换器变量类UpperCaseTransform
-
-
-
- import freemarker.template.TemplateTransformModel;
-
- public class UpperCaseTransform implements TemplateTransformModel {
-
-
-
- public Writer getWriter(Writer out, Map args) {
- return new UpperCaseWriter(out);
- }
- private class UpperCaseWriter extends Writer {
- private Writer out;
-
- UpperCaseWriter(Writer out) {
- this.out = out;
- }
- public void write(char[] cbuf, int off, int len) throws IOException {
- out.write(new String(cbuf, off, len).toUpperCase());
- }
-
- public void flush() throws IOException {
- out.flush();
- }
-
- public void close() {
-
- }
- }
- }
[java] view plain copy print ?
- "FONT-SIZE: 18px">
-
-
- import freemarker.template.TemplateTransformModel;
-
- public class UpperCaseTransform implements TemplateTransformModel {
-
-
-
- public Writer getWriter(Writer out, Map args) {
- return new UpperCaseWriter(out);
- }
- private class UpperCaseWriter extends Writer {
- private Writer out;
-
- UpperCaseWriter(Writer out) {
- this.out = out;
- }
- public void write(char[] cbuf, int off, int len) throws IOException {
- out.write(new String(cbuf, off, len).toUpperCase());
- }
-
- public void flush() throws IOException {
- out.flush();
- }
-
- public void close() {
-
- }
- }
- }
/**
* 转换器变量,自定义自己的转换器标签
*/
import freemarker.template.TemplateTransformModel;
public class UpperCaseTransform implements TemplateTransformModel {
// 转换器接口方法,将会转换标签之间的内容,首先把标签之间的内容读取到Writer对象中,
// 再由Writer对象对其中的内容施行转换处理,转换后的内容会再次存储到Writer 中
public Writer getWriter(Writer out, Map args) {
return new UpperCaseWriter(out);
}
private class UpperCaseWriter extends Writer {
private Writer out;
UpperCaseWriter(Writer out) {
this.out = out;
}
public void write(char[] cbuf, int off, int len) throws IOException {
out.write(new String(cbuf, off, len).toUpperCase());
}
public void flush() throws IOException {
out.flush();
}
// 不用调用out.close,到达结束标签close会自动被调用
public void close() {
}
}
}
二.自动生成代码应用
1.创建模板文件,在/resource/template目录下建立codeModel.ftl文件。
- package com.order.model;
-
- public class ${class} {
-
- <#list properties as prop>
- private ${prop.type} ${prop.name};
- #list>
-
- <#list properties as prop>
- public ${prop.type} get${prop.name?cap_first}(){
- return ${prop.name};
- }
- public void set${prop.name?cap_first}(${prop.type} ${prop.name}){
- this.${prop.name} = ${prop.name};
- }
- #list>
-
- }
[java] view plain copy print ?
- "FONT-SIZE: 18px">package com.order.model;
-
- public class ${class} {
-
- <#list properties as prop>
- private ${prop.type} ${prop.name};
- #list>
-
- <#list properties as prop>
- public ${prop.type} get${prop.name?cap_first}(){
- return ${prop.name};
- }
- public void set${prop.name?cap_first}(${prop.type} ${prop.name}){
- this.${prop.name} = ${prop.name};
- }
- #list>
-
- }
-
package com.order.model;
public class ${class} {
<#list properties as prop>
private ${prop.type} ${prop.name};
#list>
<#list properties as prop>
public ${prop.type} get${prop.name?cap_first}(){
return ${prop.name};
}
public void set${prop.name?cap_first}(${prop.type} ${prop.name}){
this.${prop.name} = ${prop.name};
}
#list>
}
2.应用代码生成模板文件。
- public class GenerateCodeTest {
-
- public static void main(String args[]) throws IOException,
- TemplateException {
- Configuration cfg = new Configuration();
- cfg.setDirectoryForTemplateLoading(new File(
- "D:/myspace/freemarker/resource/template/"));
- cfg.setObjectWrapper(new DefaultObjectWrapper());
-
-
- Template template = cfg.getTemplate("codeModel.ftl");
-
-
- Map root = new HashMap();
- root.put("class", "Order");
- Collection
- root.put("properties", properties);
-
-
- Map orderId = new HashMap();
- orderId.put("name", "orderId");
- orderId.put("type", "Long");
- properties.add(orderId);
-
-
- Map orderName = new HashMap();
- orderName.put("name", "orderName");
- orderName.put("type", "String");
- properties.add(orderName);
-
-
- Map money = new HashMap();
- money.put("name", "money");
- money.put("type", "Double");
- properties.add(money);
-
-
- Writer out = new OutputStreamWriter(System.out);
- template.process(root, out);
- out.flush();
-
-
- File fileDir = new File("D:/generateCodeFile");
-
- org.apache.commons.io.FileUtils.forceMkdir(fileDir);
-
- File output = new File(fileDir + "/Order.java");
- Writer writer = new FileWriter(output);
- template.process(root, writer);
- writer.close();
- }
- }
[java] view plain copy print ?
- "FONT-SIZE: 18px">public class GenerateCodeTest {
-
- public static void main(String args[]) throws IOException,
- TemplateException {
- Configuration cfg = new Configuration();
- cfg.setDirectoryForTemplateLoading(new File(
- "D:/myspace/freemarker/resource/template/"));
- cfg.setObjectWrapper(new DefaultObjectWrapper());
-
-
- Template template = cfg.getTemplate("codeModel.ftl");
-
-
- Map root = new HashMap();
- root.put("class", "Order");
- Collection> properties = new HashSet>();
- root.put("properties", properties);
-
-
- Map orderId = new HashMap();
- orderId.put("name", "orderId");
- orderId.put("type", "Long");
- properties.add(orderId);
-
-
- Map orderName = new HashMap();
- orderName.put("name", "orderName");
- orderName.put("type", "String");
- properties.add(orderName);
-
-
- Map money = new HashMap();
- money.put("name", "money");
- money.put("type", "Double");
- properties.add(money);
-
-
- Writer out = new OutputStreamWriter(System.out);
- template.process(root, out);
- out.flush();
-
-
- File fileDir = new File("D:/generateCodeFile");
-
- org.apache.commons.io.FileUtils.forceMkdir(fileDir);
-
- File output = new File(fileDir + "/Order.java");
- Writer writer = new FileWriter(output);
- template.process(root, writer);
- writer.close();
- }
- }
public class GenerateCodeTest {
public static void main(String args[]) throws IOException,
TemplateException {
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File(
"D:/myspace/freemarker/resource/template/"));
cfg.setObjectWrapper(new DefaultObjectWrapper());
/* 获取模板文件 */
Template template = cfg.getTemplate("codeModel.ftl");
/* 模板数据 */
Map root = new HashMap();
root.put("class", "Order");
Collection> properties = new HashSet>();
root.put("properties", properties);
/* 字段1 orderId */
Map orderId = new HashMap();
orderId.put("name", "orderId");
orderId.put("type", "Long");
properties.add(orderId);
/* 字段2 orderName */
Map orderName = new HashMap();
orderName.put("name", "orderName");
orderName.put("type", "String");
properties.add(orderName);
/* 字段3 money */
Map money = new HashMap();
money.put("name", "money");
money.put("type", "Double");
properties.add(money);
/* 生成输出到控制台 */
Writer out = new OutputStreamWriter(System.out);
template.process(root, out);
out.flush();
/* 生成输出到文件 */
File fileDir = new File("D:/generateCodeFile");
// 创建文件夹,不存在则创建
org.apache.commons.io.FileUtils.forceMkdir(fileDir);
// 指定生成输出的文件
File output = new File(fileDir + "/Order.java");
Writer writer = new FileWriter(output);
template.process(root, writer);
writer.close();
}
}
三.JSP调用模板文件输出。
1.首先创建模板文件,在WEB-INF/templates目录下建立test.ftl文件。
[java] view plain copy print ?
- "FONT-SIZE: 18px">Hello,${name}!
-
Hello,${name}!
2.解析模板文件类,传入pageContext参数。
- public class FreemarkerTest {
-
- public void execute(PageContext pageContext) throws Exception
- {
- Configuration cfg = new Configuration();
-
- cfg.setServletContextForTemplateLoading(pageContext.getServletContext(),
- "WEB-INF/templates");
- Map root = new HashMap();
- root.put("name", "Tom");
-
- Template t = cfg.getTemplate("test.ftl");
- Writer out = pageContext.getResponse().getWriter();
- t.process(root, out);
- }
- }
[java] view plain copy print ?
- "FONT-SIZE: 18px">public class FreemarkerTest {
-
- public void execute(PageContext pageContext) throws Exception
- {
- Configuration cfg = new Configuration();
-
- cfg.setServletContextForTemplateLoading(pageContext.getServletContext(),
- "WEB-INF/templates");
- Map root = new HashMap();
- root.put("name", "Tom");
-
- Template t = cfg.getTemplate("test.ftl");
- Writer out = pageContext.getResponse().getWriter();
- t.process(root, out);
- }
- }
-
public class FreemarkerTest {
public void execute(PageContext pageContext) throws Exception
{
Configuration cfg = new Configuration();
// 设置模板的路径
cfg.setServletContextForTemplateLoading(pageContext.getServletContext(),
"WEB-INF/templates");
Map root = new HashMap();
root.put("name", "Tom");
// 获取模板文件
Template t = cfg.getTemplate("test.ftl");
Writer out = pageContext.getResponse().getWriter();
t.process(root, out);
}
}
3.创建JSP文件调用输出。
- <%@ page language="java" contentType="text/html; charset=GB2312"
- pageEncoding="GB2312"%>
- <%@page import="com.freemarker.test.FreemarkerTest;"%>
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GB2312">
- <title>HelloWorldtitle>
- head>
- <body>
- <%
- FreemarkerTest c1 = new FreemarkerTest();
- c1.execute(pageContext);
- %>
- body>
- html>
[html] view plain copy print ?
- <SPAN style="FONT-SIZE: 18px"><%@ page language="java" contentType="text/html; charset=GB2312"
- pageEncoding="GB2312"%>
- <%@page import="com.freemarker.test.FreemarkerTest;"%>
-
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GB2312">
- <title>HelloWorldtitle>
- head>
- <body>
- <%
- FreemarkerTest c1 = new FreemarkerTest();
- c1.execute(pageContext);
- %>
- body>
- html>SPAN>
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<%@page import="com.freemarker.test.FreemarkerTest;"%>
HelloWorld
<%
FreemarkerTest c1 = new FreemarkerTest();
c1.execute(pageContext);
%>
四.Servlet调用模板文件生成html文件进行页面展示。
1.创建模板文件,在WebRoot/index目录下建立test.ftl文件。
-
-
- 用户名:${userName}
- 邮 箱:${email}
-
-
[java] view plain copy print ?
- "FONT-SIZE: 18px">
-
- 用户名:${userName}
- 邮 箱:${email}
-
-
用户名:${userName}
邮 箱:${email}
2.创建Servlet文件解析模板文件并生成html文件进行页面输出。
- public class FreemarkerServletTest extends HttpServlet {
-
- public FreemarkerServletTest() {
- super();
- }
- public void destroy() {
- super.destroy();
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- String path = getServletContext().getRealPath("/") + "index.htm";
- System.out.println(path);
- try {
- freemarker(request, "test.ftl", path, "index");
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- request.getRequestDispatcher("/index.htm").forward(request, response);
- }
-
-
-
-
-
-
-
-
-
- public void freemarker(HttpServletRequest request,String ftl,
- String html,String file) throws Exception {
- Configuration cfg = new Configuration();
-
- cfg.setServletContextForTemplateLoading(getServletContext(),"/" + file);
- cfg.setEncoding(Locale.getDefault(), "GB18030");
-
-
- Template tep = cfg.getTemplate(ftl);
- tep.setEncoding("GB18303");
-
-
- Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(html),"GB18030"));
-
- Map map = new HashMap();
- map.put("userName", "wxj");
- map.put("email", "[email protected]");
-
- tep.process(map, out);
- }
-
- public void init() throws ServletException {
- }
- }
[java] view plain copy print ?
- "FONT-SIZE: 18px">public class FreemarkerServletTest extends HttpServlet {
-
- public FreemarkerServletTest() {
- super();
- }
- public void destroy() {
- super.destroy();
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- String path = getServletContext().getRealPath("/") + "index.htm";
- System.out.println(path);
- try {
- freemarker(request, "test.ftl", path, "index");
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- request.getRequestDispatcher("/index.htm").forward(request, response);
- }
-
-
-
-
-
-
-
-
-
- public void freemarker(HttpServletRequest request,String ftl,
- String html,String file) throws Exception {
- Configuration cfg = new Configuration();
-
- cfg.setServletContextForTemplateLoading(getServletContext(),"/" + file);
- cfg.setEncoding(Locale.getDefault(), "GB18030");
-
-
- Template tep = cfg.getTemplate(ftl);
- tep.setEncoding("GB18303");
-
-
- Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(html),"GB18030"));
-
- Map map = new HashMap();
- map.put("userName", "wxj");
- map.put("email", "[email protected]");
-
- tep.process(map, out);
- }
-
- public void init() throws ServletException {
- }
- }
public class FreemarkerServletTest extends HttpServlet {
public FreemarkerServletTest() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//生成html后的文件
String path = getServletContext().getRealPath("/") + "index.htm";
System.out.println(path);
try {
freemarker(request, "test.ftl", path, "index");
} catch (Exception e) {
e.printStackTrace();
}
//跳转到刚生成的html文件中
request.getRequestDispatcher("/index.htm").forward(request, response);
}
/**
* 生成静态文件
* @param request
* @param ftl ftl文件
* @param html 生成html后的文件
* @param file 存放ftl文件的路径
* @throws Exception
*/
public void freemarker(HttpServletRequest request,String ftl,
String html,String file) throws Exception {
Configuration cfg = new Configuration();
// 设置加载模板的路径
cfg.setServletContextForTemplateLoading(getServletContext(),"/" + file);
cfg.setEncoding(Locale.getDefault(), "GB18030");
//获得模板并设置编码
Template tep = cfg.getTemplate(ftl);
tep.setEncoding("GB18303");
//新建输出,生成html文件
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(html),"GB18030"));
//设置值
Map map = new HashMap();
map.put("userName", "wxj");
map.put("email", "[email protected]");
// 解析并输出
tep.process(map, out);
}
public void init() throws ServletException {
}
}
3.web.xml文件进行servlet配置。
- <servlet>
- <servlet-name>FreemarkerServletTestservlet-name>
- <servlet-class>com.servlet.FreemarkerServletTestservlet-class>
- servlet>
-
- <servlet-mapping>
- <servlet-name>FreemarkerServletTestservlet-name>
- <url-pattern>/servlet/FreemarkerServletTesturl-pattern>
- servlet-mapping>
[xml] view plain copy print ?
- <SPAN style="FONT-SIZE: 18px"><servlet>
- <servlet-name>FreemarkerServletTestservlet-name>
- <servlet-class>com.servlet.FreemarkerServletTestservlet-class>
- servlet>
-
- <servlet-mapping>
- <servlet-name>FreemarkerServletTestservlet-name>
- <url-pattern>/servlet/FreemarkerServletTesturl-pattern>
- servlet-mapping>SPAN>
FreemarkerServletTest
com.servlet.FreemarkerServletTest
FreemarkerServletTest
/servlet/FreemarkerServletTest
4.页面访问如下Url:
http://localhost:8080/freemarkerWeb/servlet/FreemarkerServletTest
输出结果如下:
- 用户名:wxj
- 邮 箱:jxauwxj@126.com
[java] view plain copy print ?
- "FONT-SIZE: 18px">用户名:wxj
- 邮 箱:jxauwxj@126.com
同时会在部署环境的目录下生成index.htm文件