读书人

一个简略的FreeMarker案例

发布时间: 2012-11-22 00:16:41 作者: rapoo

一个简单的FreeMarker案例

1,首先:导入FreeMarker的Jar文件

2,写一个JavaBean用于封装数据

package freemarker;

public class UserInfo {

private String uname=null;

private int uage = 0;

public String getUname() {

return uname;

}

public void setUname(String uname) {

this.uname = uname;

}

public int getUage() {

return uage;

}

public void setUage(int uage) {

this.uage = uage;

}

}

3,写一个测试用的Test

package freemarker;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import freemarker.template.Configuration;

import freemarker.template.Template;

public class FreeMarkerTest {

private Configuration cfg = null;

public Configuration getConf(){

return cfg;

}

public void init() throws Exception {

cfg = new Configuration();

cfg.setDirectoryForTemplateLoading(new File("bin/freemarker"));//模板文件所在的位置

}

public static void main(String[] args) throws Exception {

FreeMarkerTest obj = new FreeMarkerTest();

obj.init();

Map root = new HashMap();

UserInfo u = new UserInfo();

u.setUname("熊师虎");

u.setUage(100);

List strList = new ArrayList();

strList.add("aa");

strList.add("bb");

strList.add("cc");

strList.add("dd");

root.put("strlist", strList);

root.put("u", "u");//放入用户的信息,在模板中可以取到用户的信息

root.put("htag", "<h1>我是一级标题</h1>");

Template t = obj.getConf().getTemplate("Test.ftl");

//在WebRoot目录下构建一个test1.html

Writer out = new OutputStreamWriter(new FileOutputStream("WebRoot/eg.html"),"GBK");

t.process(root, out);//开始转化处理

System.out.println("Successfull.....");

}

}

4,写一个Servlet

package control;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import freemarker.UserInfo;

import freemarker.template.Configuration;

import freemarker.template.Template;

import freemarker.template.TemplateException;

public class FreeMarkerServletextends HttpServlet {

public FreeMarkerServlet() {

super();

}

@Override

public void destroy() {

super.destroy();

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

Configuration cfg=new Configuration();

//String path=this.getServletContext().getRealPath("bin/freemarker");

//cfg.setDirectoryForTemplateLoading(new File(path));

cfg.setServletContextForTemplateLoading(this.getServletContext(), "bin/freemarker");

Template tp=cfg.getTemplate("test.ftl");

//因为这个是一个Servlet,所以要用这个,并且由于在Content-Type中配置的字符集都是UTF-8,所以在此处要也设置成UTF-8

Writer out=new OutputStreamWriter(new FileOutputStream(this.getServletContext().getRealPath(".")+"/eg.html"),"utf-8"); //注意的是这里的字符集定要和自己配置的字符集是相同的,否则将出现乱码。并且注意OutputStreamWriter转为Writer的方法。

//PrintWriter out=new PrintWriter(new FileOutputStream(new File(this.getServletContext().getRealPath(".")+"/eg.html")));////如果为servelet则response.getOutputStream()

Map root = new HashMap();

UserInfo u=new UserInfo();

u.setUname("熊师虎");

u.setUage(100);

root.put("u", u);//放入用户的信息,在模板中可以取到用户的信息

List strlist=new ArrayList();

strlist.add("aa");

strlist.add("bb");

strlist.add("cc");

strlist.add("dd");

root.put("strlist", strlist);

root.put("htag", "<h1>我是一级标题</h1>");

try {

tp.process(root, out);

} catch (TemplateException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("Successfull................");

out.flush();

out.close();

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

5、配置servlet的url-pattern为:FreeMarkerServlet

6、运行FreeMarkerTest或访问FreeMarkerServlet,用以生成e.html

7、访问e.html,即在浏览器中输入:http://localhost:8080/freemarker/eg.html

读书人网 >互联网

热点推荐