原文地址:http://www.cnblogs.com/hqr9313/archive/2012/08/24/2654109.html
1.把包lib/freemarker.jar拷贝到项目中
2.新建模板文件WEB-INF/templates/test.ftl,内容如下:
Hello,${name}!
3.新建一个操作类Class1.java,(把模板装载到jsp页面中). 内容如下
package com.abc.web;
import java.io.Writer; import java.util.HashMap; import java.util.Map;
import javax.servlet.jsp.PageContext;
import freemarker.template.Configuration; import freemarker.template.Template; public class Class1 { 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); } }
4.新建一个jsp页面test1.jsp,内容如下:
<%@ page language="java" contentType="text/html; charset=GB2312" pageEncoding="GB2312"%><%@ pageimport="com.abc.web.Class1"%><!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=GB2312"><title>Insert title here</title></head><body><%Class1 c1 =new Class1(); c1.execute(pageContext); %></body></html>
页面的显示效果为:
Hello,Tom!
2. 在WEB-INF下新建文件夹templates 在templates下新建test.ftl文件
内容为:
<html>
<head> <title>Hello Word</title> </head>
<body>
<h3>${message},${name}</h3>
</body>
</html>
3. 新建Servlet,内容如下:
package com.njy.freemarker.servlet;
import java.io.IOException; 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.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException;
public class HelloFreeMarker extends HttpServlet {
public HelloFreeMarker() { super(); }
//负责管理FreeMarker模板的Configuration实例 private Configuration cfg = null; public void init() throws ServletException { //创建一个FreeMarker实例 cfg = new Configuration(); //指定FreeMarker模板文件的位置 cfg.setServletContextForTemplateLoading(getServletContext(),"/WEB-INF/templates"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //建立数据模型 Map root = new HashMap(); root.put("message", "hello world"); root.put("name", "聂靖宇"); root.put("personList", list); //获取模板文件 Template t = cfg.getTemplate("test.ftl"); //开始准备生成输出 //- 使用模板文件的Charset作为本页面的charset //- 使用text/html MIME-type response.setContentType("text/html; charset=" + t.getEncoding()); Writer out = response.getWriter(); //合并数据模型和模板,并将结果输出到out中 try { t.process(root, out); // 往模板里写数据 } catch (TemplateException e) { e.printStackTrace(); } }
public void destroy() { super.destroy(); } } 4web.xml中配置servlet
<servlet> <servlet-name>HelloFreeMarker</servlet-name> <servlet-class>com.njy.freemarker.servlet.HelloFreeMarker</servlet-class> </servlet>
<servlet-mapping> <servlet-name>HelloFreeMarker</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
5. 运行http://localhost:8080/freemarkerTest/hello,页面效果如下如所示:
hello world,聂靖宇
小一 11 11.01 小二 12 12.02 小三 13 13.03 小四 14 14.04 小五 15 15.05 this is a Child! this is a Child! this is a Child! this is a Child! this is a Child!
-----------------------------------------------------一些相关知识点-------------------------------------------------------------
1模板+数据模型=输出
2数据模型一览
<html> <head> <title>Welcome!</title> </head> <body> <h1>Welcome ${user}!</h1> <p>Our latest product: <a href="${latestProduct.url}">${latestProduct.name}</a>! </body> </html>
模板能用的所有数据被包装成data-model数据模型。 (root) | +- user = "Big Joe" | +- latestProduct | +- url = "products/greenmouse.html" | +- name = "green mouse"