Total Pageviews

Saturday 24 March 2012

freemarker实例,一步一步教你搭建环境和配置

下面演示一个简单的使用项目过程:
1. 使用myeclipse创建一个web项目testFM
2. 把下载到的jar包(freemarker-2.3.9.jar)放到\WebRoot\WEB-INF\lib目录下。下载地 址:http://freemarker.sourceforge.net/freemarkerdownload.html (注:官方网站.org的经常打不开)
3. 在WebRoot下面新建templates文件包,然后在里面新建扩展名为ftl的模板。
a) 创建hello.ftl内容如下:
1
2
3
4
5
6
7
8
<html>
    <head>
    <title>hello!</title>
    </head>
    <body>
    <h1>hello ${user}!</h1>
    </body>
</html>

4. 在src目录下创建example包 和Hello.java文件,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package example.fm;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
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;
 
/**
 * Servlet implementation class Hello
 */
public class Hello extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
    private Configuration cfg;
     
     
    @Override
    public void init() throws ServletException {
        //初始化FreeMarker配置
        cfg = new Configuration();
        //设置FreeMarker的模版文件位置
        cfg.setServletContextForTemplateLoading(getServletContext(), "templates"); 
    }
 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Hello() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //建立数据模型
        Map root = new HashMap();
        //放入对应数据key value
        root.put("user", "zdz");
        //取得模版文件
        Template t = cfg.getTemplate("hello.ftl");
        //开始准备生成输出
        //使用模版文件的charset作为本页面的charset
         //使用text/html MIME-type
        response.setContentType("text/html;charset=" + t.getEncoding());
        PrintWriter out = response.getWriter();
         
         //合并数据模型和模版,并将结果输出到out中
        try {
            t.process(root, out);// 用模板来开发servlet可以只在代码里面加入动态的数据
        } catch (TemplateException  e) {
            // TODO: handle exception
             throw new ServletException("处理Template模版中出现错误", e);
        }
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
 
}
5.配置web.xml 代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
  <display-name>testFM</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Hello</display-name>
    <servlet-name>Hello</servlet-name>
    <servlet-class>example.fm.Hello</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/Hello</url-pattern>
  </servlet-mapping>
</web-app>

6.在WebRoot下面新建引导页面index.html,代码如下:
1
2
3
4
5
6
7
8
9
10
11
<html>
<head>
<title>Hello FreeMarker Example</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
</head>
<body>
    点击下面链接看看效果:
    <hr>
    <a href="hello">调用Hello模板</a>
</body>
</html>

点击下面链接看看效果:
<hr>
<a href="hello">调用Hello模板</a>
</body>
</html>
7.把testFM项目部署到tomcate并启动服务。
8.打开浏览器,输入地址:http://localhost:8080/testFM/
9.点击调用Hello模板 链接,如果出现hello zdz!则说明模板调用成功了!

No comments:

Post a Comment