1. 模板文件 test04.ftl
foo<@customUpper> bar f <#-- 这里同意使用全部的 FTL --> <#list ["red", "green", "blue"] as color> ${ color} baaz 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 的用户自己定义指令在逐步改变* 它嵌套内容的输出转换为大写形式*指令内容
*指令參数:无*
循环变量:无*
指令嵌套内容:是*/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 BAAZwombat