1、java应用程序中
我用maven导入FreeMarker的jar包
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.8</version>
</dependency>
package com.test;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class Test {
/** *//**
* @param args
*/
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.setObjectWrapper(new DefaultObjectWrapper());
configuration.setTemplateLoader(new ClassTemplateLoader(Test.class, "/com/test"));
try {
Template template = configuration.getTemplate("temp.ftl");
StringWriter writer = new StringWriter();
Map<String, Object> context = new HashMap<String, Object>();
context.put("message", "我的第一个FreeMarker程序");
template.process(context, writer);
System.out.println(writer.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这里是用java应用程序,需要加入下面两句(设置模板文件载入):
方法一:
configuration.setObjectWrapper(new DefaultObjectWrapper());
configuration.setTemplateLoader(new ClassTemplateLoader(Test.class, "/com/test"));
其中"/com/test" 是模板文件所在的文件夹,前面的“/”必须,这里我的模板文件和当前java类在同一路径下,则这样写,如果你的模板文件在src跟目录下,这里只需要写“/”就可以了
context.put("message", "我的第一个FreeMarker程序");
是模板文件中用到的变量,map中的变量可以是javabean,也可以是对象
方法二:
configuration.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/templates");
这里主要针对Servlet的时候,当然在Servlet中也可以用方法一提到的写法
package com.newegg.lab.freemarker.servlet;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/** *//**
* Servlet implementation class for Servlet: FreemarkerServlet
*
*/
public class FreemarkerServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
private Configuration configuration;
private Template template;
/**//*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public FreemarkerServlet() {
super();
}
@Override
public void destroy() {
// TODO Auto-generated method stub
configuration = null;
}
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
configuration = new Configuration();
configuration.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/templates");
// configuration.setObjectWrapper(new DefaultObjectWrapper());
// configuration.setTemplateLoader(new ClassTemplateLoader(FreemarkerServlet.class, "/"));
}
/**//*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**//*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Map root = new HashMap();
root.put("message", "Hello World!的的的的");
// Get the templat object
Template t = configuration.getTemplate("test.ftl");
// Prepare the HTTP response:
// - Use the charset of template for the output
// - Use text/html MIME-type
response.setContentType("text/html; charset=" + t.getEncoding());
Writer out = response.getWriter();
// Merge the data-model and the template
try {
t.process(root, out);
StringWriter stringWriter = new StringWriter();
t.process(root, stringWriter);
System.out.println(stringWriter.toString());
} catch (TemplateException e) {
throw new ServletException(
"Error while processing FreeMarker template", e);
}
}
}
模板文件中需要使用 ${}将java类中在Map对象中声明的变量包围起来就可以了
例如模板文件:
<pre>
//描述信息是 : ${message}
</pre>
则得到的结果是:
<pre>
//描述信息是 : Hello World!的的的的
</pre>