zoukankan      html  css  js  c++  java
  • freemarke之TemplateDirectiveModel详解

    http://hougbin.iteye.com/blog/1457924

    TemplateDirectiveModel接口是freemarker自定标签或者自定义指令的核心处理接口。通过实现该接口,用户可以自定义标签(指令)进行任意操作,、 任意文本写入模板的输出。

         该接口中只定义了如下方法,当模板页面遇到用户自定义的标签指令时,该方法会被执行。

    public void execute(Environment env, Map params, TemplateModel[] loopVars, 
                TemplateDirectiveBody body) throws TemplateException, IOException;

    @param env:系统环境变量,通常用它来输出相关内容,如Writer out = env.getOut();

    @param params:自定义标签传过来的对象,其key=自定义标签的参数名,value值是TemplateModel类型,而TemplateModel是一个接口类型,通常我们都使用TemplateScalarModel接口来替代它获取一个String 值,如TemplateScalarModel.getAsString();当然还有其它常用的替代接口,如TemplateNumberModel获取number,TemplateHashModel等

    @param loopVars  循环替代变量

    @param body 用于处理自定义标签中的内容,如<@myDirective>将要被处理的内容</@myDirective>;当标签是<@myDirective />格式时,body=null

    ==========================================

    下面将详细讲解如何用java实现自定义标签

    第一步:freemarker基本配置

              Configuration cfg = new Configuration();

     第二步:获取模板

            Template tt = cfg.getTemplate("你定义的ftl模板的位置", "UTF-8");

    第三部:

  • 相关阅读:
    (一)python 简单网页爬虫
    环形队列的应用
    AutoResetEvent 和 ManualResetEvent 多线程应用
    委托 和 事件
    Action 和 Func 的用法以及区别
    IIS 配置缓存
    IIS 发布双证书
    函数中返回局部变量的问题
    python函数2-函数参数
    Python基础语法6-冒泡排序
  • 原文地址:https://www.cnblogs.com/a757956132/p/4647839.html
Copyright © 2011-2022 走看看