FreeMarker 自定义指令(三)

1. 模板文件 test04.ftl

foo
<@customUpper>
    bar f
    <#-- 这里允许使用所有的 FTL -->
    <#list ["red", "green", "blue"] as color>
        ${color}
    </#list>
    baaz
</@customUpper>
wombat

2. 自定义指令类 UpperDirective.java

ps: 一个把字符串变为大写的指令

package com.freemarker.test04.Directives;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

/**
* FreeMarker 的用户自定义指令在逐步改变
* 它嵌套内容的输出转换为大写形式
* <p><b>指令内容</b></p>
* <p>指令参数:无
* <p>循环变量:无
* <p>指令嵌套内容:是
*/
public class UpperDirective implements TemplateDirectiveModel {

    public void execute(Environment env,
            Map params, TemplateModel[] loopVars,
            TemplateDirectiveBody body)
            throws TemplateException, IOException {
        // 检查参数是否传入
        if (!params.isEmpty()) {
            throw new TemplateModelException(
                    "This directive doesn't allow parameters.");
        }
        if (loopVars.length != 0) {
                throw new TemplateModelException(
                    "This directive doesn't allow loop variables.");
        }

        // 是否有非空的嵌入内容
        if (body != null) {
            // 执行嵌入体部分,和 FTL 中的<#nested>一样,除了
            // 我们使用我们自己的 writer 来代替当前的 output writer.
            body.render(new UpperCaseFilterWriter(env.getOut()));
        } else {
            throw new RuntimeException("missing body");
        }
    }

    /**
     * {@link Writer}改变字符流到大写形式,
     * 而且把它发送到另外一个{@link Writer}中。
     */
    private static class UpperCaseFilterWriter extends Writer {

        private final Writer out;

        UpperCaseFilterWriter (Writer out) {
            this.out = out;
        }

        @Override
        public void write(char[] cbuf, int off, int len)throws IOException {
            char[] transformedCbuf = new char[len];
            for (int i = 0; i < len; i++) {
                transformedCbuf[i] = Character.toUpperCase(cbuf[i + off]);
            }
            out.write(transformedCbuf);
        }

        public void flush() throws IOException {
            out.flush();
        }

        public void close() throws IOException {
            out.close();
        }
    }

}

3. 测试类文件 Test.java

package com.freemarker.test04.Directives;

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

public class Test {

    public static void main(String[] args) throws Exception {

        // 创建 Freemarker 配置实例
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
        // 指定模板文件从何处加载的数据源,这里设置成一个文件目录。
        cfg.setDirectoryForTemplateLoading(new File("templates"));
        cfg.setDefaultEncoding("UTF-8");
        // 简单地重新抛出异常; 这应该在大多数生产系统中使用。
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        // 创建一个数据模型
        Map root = new HashMap();
        // 测试自定义指令 --------------------------------
        root.put("customUpper", new UpperDirective());

        // 获取模板(使用内部缓存)
        Template temp = cfg.getTemplate("test04.ftl");

        // 合并数据模型模板
        Writer out = new OutputStreamWriter(System.out);
        temp.process(root, out);
        out.flush();
        out.close();
        // 注意: ------------
        // 为了简单起见,这里压制了异常(在方法签名中声明了异常,译者注),而在正式运行的产品中不要这样做。
    }
}

运行结果

foo
    BAR F
        RED
        GREEN
        BLUE
    BAAZ
wombat
自定义一个名为 ListDirective 的 FreeMarker 指令,你需要遵循以下步骤: 1. 创建一个 Java 类来实现指令。这个类应该继承 `freemarker.template.TemplateDirectiveModel` 接口,并且需要实现 `execute()` 方法,该方法会被 FreeMarker 引擎调用来处理指令。 ```java import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import freemarker.core.Environment; import freemarker.template.*; public class ListDirective implements TemplateDirectiveModel { public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, java.io.IOException { // 在这里编写逻辑来处理指令 // ... } } ``` 2. 在 `execute()` 方法中编写处理逻辑。在这里,你需要解析和验证指令参数,并使用 `TemplateModel` 对象来获取指令所在的模板环境和输出流。你也需要创建一个 `SimpleScalar` 对象来包装列表数据,并将其放入模板环境中。 ```java public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, java.io.IOException { // 解析和验证指令参数 String listName = DirectiveUtils.getRequiredParam(params, "name"); Object listData = DirectiveUtils.getRequiredParam(params, "data"); // 获取模板环境和输出流 Writer out = env.getOut(); PageContext pageContext = (PageContext) env.getCustomAttribute("PageContext"); if (pageContext == null) { throw new JspException("PageContext not found"); } // 创建 SimpleScalar 对象来包装列表数据 SimpleScalar listModel = new SimpleScalar(listData.toString()); // 将 SimpleScalar 对象放入模板环境中 env.setVariable(listName, listModel); // 处理指令内容 if (body != null) { body.render(out); } } ``` 3. 将自定义指令注册到 FreeMarker 引擎中。你可以使用 `Configuration` 对象的 `setSharedVariable()` 方法将指令实例添加到引擎中。 ```java import freemarker.template.Configuration; Configuration cfg = new Configuration(Configuration.VERSION_2_3_30); cfg.setSharedVariable("list", new ListDirective()); ``` 现在,你就可以在模板中使用自定义指令了: ``` <@list name="myList" data=[1, 2, 3]> <#list myList as item> ${item} </#list> </@list> ``` 这将输出: ``` 1 2 3 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值