FreeMarker快速上手

创建Configuration实例

首先必须创建一个freemarker.template.Configuration 实例并调整其设置。Configuration 实例保存freemarker的设置,同时处理预解析的模板的创建和缓存。

通常应用程序的生命周期中只会创建一个Configuration实例。

Configuration cfg = new Configuration();
// 指定模板文件的数据源,这里是一个文件目录。

cfg.setDirectoryForTemplateLoading(
        new File("/where/you/store/templates"));
// 指定模板如何发现数据模型,这是一个高级主题,暂且这样使用。
cfg.setObjectWrapper(new DefaultObjectWrapper());  

目前我们使用单个的Configuration实例。不过如果一个系统有多个独立的组件使用FreeMarker,它们会使用各自的Configuration 实例。 

创建数据模型

我们可以简单地使用java.lang java.util 和自定义的JavaBean构建对象模型,比如我们构建数据模型如下: 

(root)
  |
  +- user = "Big Joe"
  |
  +- latestProduct
      |
      +- url = "products/greenmouse.html"

      |
      +- name = "green mouse"  

如下是构建数据模型的代码:

// Create the root hash
Map root = new HashMap();
// Put string ``user'' into the root
root.put("user", "Big Joe");
// Create the hash for ``latestProduct''
Map latest = new HashMap();
// and put it into the root
root.put("latestProduct", latest);
// put ``url'' and ``name'' into latest
latest.put("url", "products/greenmouse.html");
latest.put("name", "green mouse");  

也可以使用一个包含urlname 属性的JavaBean实例表示lastestProduct。

获取模板

模板通过freemarker.template.Template实例表示。通常从Configuration 实例中获取Template实例,任何时候都可以调用getTemplate方法获取一个Template 实例。假定模板文件test.ftl 保存在先前设置的目录中:

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

上述代码将读取,解析/where/you/store/templates/test.ftl文件,创建一个对应的Template实例 。

Configuration 缓存Template 实例, 因此当需要再次获取test.ftl 文件, 将不会创建一个新的Template实例。

合并模板和数据模型

就我们所知,数据模型+模板=输出,通过调用模板的process 方法合并数据模型和模板,process. 方法接受一个数据模型根和一个writer作为参数,将结果输出到Writer。 为简化起见,这里输出到控制台。

Writer out = new OutputStreamWriter(System.out);
temp.process(root, out);
out.flush();  

一旦获取一个Template 实例,可以合并不同的数据模型和一个模板(Template 实例基本上是无状态的),而test.ftl只会当Template 实例被创建的时候访问一次。

整合

这是先前代码片断的源文件,不要忘记将freemarker.jar放在CLASSPATH.中。

import freemarker.template.*;
import java.util.*;
import java.io.*;

public class Test {

    public static void main(String[] args) throws Exception {
        
        /* ------------------------------------------------------------------- */    
        /* You usually do it only once in the whole application life-cycle:    */    
    
        /* Create and adjust the configuration */
        Configuration cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(
                new File("/where/you/store/templates"));
        cfg.setObjectWrapper(new DefaultObjectWrapper());

        /* ------------------------------------------------------------------- */    
        /* You usually do these for many times in the application life-cycle:  */    
                
        /* Get or create a template */
        Template temp = cfg.getTemplate("test.ftl");

        /* Create a data model */
        Map root = new HashMap();
        root.put("user", "Big Joe");
        Map latest = new HashMap();
        root.put("latestProduct", latest);
        latest.put("url", "products/greenmouse.html");
        latest.put("name", "green mouse");

        /* Merge data model with template */
        Writer out = new OutputStreamWriter(System.out);
        temp.process(root, out);
        out.flush();
    }
}  

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值