FreeMarker简介

jar包地址:http://www.freemarker.org/freemarkerdownload.html
FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。
FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。
它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。

Demo:
一、在eclipse中创建FreeMarker模板

${user.userName}
${user.userPassword}

并将其命名为“user.ftl”

二、在eclipse中创建FreeMarker数据模型
以User为例:

 1  package  test.client;
 2 
 3  /**
 4   * 用户实体类
 5   * 
 6   *  @author  Ying-er
 7   * @time 2010-2-6下午04:05:25
 8   *  @version  1.0
 9    */
10  public   class  User {
11       private  String userName;
12 
13       private  String userPassword;
14 
15       public  String getUserName() {
16           return  userName;
17      }
18 
19       public   void  setUserName(String userName) {
20           this .userName  =  userName;
21      }
22 
23       public  String getUserPassword() {
24           return  userPassword;
25      }
26 
27       public   void  setUserPassword(String userPassword) {
28           this .userPassword  =  userPassword;
29      }
30 
31  }
32 


注意:注意:FreeMarker数据模型不是文本文件,而是树状结构的。

三、在eclipse中填充FreeMarker数据模型
将创建好的User对象以key-value的形式封装到Map中
片段代码:

User user  =   new  User();
        user.setUserName(
" 测试 " );
        user.setUserPassword(
" 123 " );

        Map
< String, Object >  root  =   new  HashMap < String, Object > ();
        root.put(
" user " , user);


四、创建FreeMarker的模板引擎,解析模板
1.创建和配置Configuration对象,Configuration对象实例负责管理FreeMarker模板的路径加载及模板的创建和缓存。
  通常应用程序的生命周期中只会创建一个Configuration实例。
2.获取模板实例,即通过Configuration实例获取Template实例,调用getTemplate()方法。
3.合并数据模型和模板

该步骤完整代码:

 1  package  test.freemarker.util;
 2 
 3  import  java.io.File;
 4  import  java.io.IOException;
 5  import  java.io.OutputStreamWriter;
 6  import  java.io.Writer;
 7  import  java.util.Map;
 8 
 9  import  freemarker.template.Configuration;
10  import  freemarker.template.DefaultObjectWrapper;
11  import  freemarker.template.Template;
12  import  freemarker.template.TemplateException;
13 
14  /**
15   * freemarker 模板工具
16   * 
17   *  @author  Ying-er
18   * @time 2010-2-6下午04:07:27
19   *  @version  1.0
20    */
21  public   class  FreeMarkertUtil {
22       /**
23       * 
24       *  @param  templateName
25       *            模板文件名称
26       *  @param  templateEncoding
27       *            模板文件的编码方式
28       *  @param  root
29       *            数据模型根对象
30        */
31       public   static   void  analysisTemplate(String templateName,
32              String templateEncoding, Map <? ?>  root) {
33           try  {
34               /**
35               * 创建Configuration对象
36                */
37              Configuration config  =   new  Configuration();
38               /**
39               * 指定模板路径
40                */
41              File file  =   new  File( " templates " );
42               /**
43               * 设置要解析的模板所在的目录,并加载模板文件
44                */
45              config.setDirectoryForTemplateLoading(file);
46               /**
47               * 设置包装器,并将对象包装为数据模型
48                */
49              config.setObjectWrapper( new  DefaultObjectWrapper());
50 
51               /**
52               * 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
53                */
54              Template template  =  config.getTemplate(templateName,
55                      templateEncoding);
56               /**
57               * 合并数据模型与模板
58                */
59              Writer out  =   new  OutputStreamWriter(System.out);
60              template.process(root, out);
61              out.flush();
62              out.close();
63          }  catch  (IOException e) {
64              e.printStackTrace();
65          }  catch  (TemplateException e) {
66              e.printStackTrace();
67          }
68 
69      }
70  }
71 
摘自:http://www.blogjava.net/crazycoding/archive/2010/02/06/312192.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值