freemarker自定义标签的写法和应用

    添加时间:2013-5-25 点击量:

    起首是freemarker的jar包:地址


    写标签类(须要持续TemplateDirectiveModel接口重写execute办法)



    package freemarker;
    

    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.TemplateNumberModel;

    public class LabelDirective implements TemplateDirectiveModel {

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars,
    TemplateDirectiveBody body)
    throws TemplateException, IOException {
    Writer out
    = env.getOut();

    //将模版里的数字参数转化成int类型的办法,,其它类型的转换请看freemarker文档
    TemplateModel paramValue = (TemplateModel) params.get(num);
    int num = ((TemplateNumberModel) paramValue).getAsNumber().intValue();

    out.write(
    Akishimo num= + params.get(num)+的类型为:+paramValue.getClass());
    if (body != null) {
    body.render(env.getOut());
    }
    else{
    throw new RuntimeException(标签内部至少要加一个空格);
    }
    }
    }


    写设备和测试的类:



     1 package freemarker;
    
    2
    3 import java.io.BufferedWriter;
    4 import java.io.File;
    5 import java.io.FileOutputStream;
    6 import java.io.OutputStreamWriter;
    7 import java.io.Writer;
    8 import java.util.HashMap;
    9 import java.util.Map;
    10
    11 import freemarker.template.Configuration;
    12 import freemarker.template.DefaultObjectWrapper;
    13 import freemarker.template.Template;
    14
    15 public class Test {
    16
    17 public static void main(String[] args) throws Exception {
    18
    19 Configuration cfg = new Configuration();
    20 //将写好的标签参加,起名叫label
    21 cfg.setSharedVariable(label, new LabelDirective());
    22 cfg.setDirectoryForTemplateLoading(new File(temp));
    23 cfg.setObjectWrapper(new DefaultObjectWrapper());
    24
    25 Template temp = cfg.getTemplate(list.ftl);
    26
    27 Map root = new HashMap();
    28 root.put(user, rzy);
    29
    30 / Writer out = new OutputStreamWriter(System.out); /
    31
    32 File htmlFile = new File(E:/a.html);
    33 Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), UTF-8));
    34
    35 temp.process(root, out);
    36 out.flush();
    37 }
    38 }


    模版list.ftl先这么写



    <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
    
    <html xmlns=http://www.w3.org/1999/xhtml>
    <head>
    <meta http-equiv=Content-Type content=text/html; charset=utf-8 />
    <title>无题目文档</title>
    </head>

    <body>
    <h1>¥{user}自定义标签输出成果</h1><br/>
    <@label num=12>

    </@label>
    </body>
    </html>


    运行后打开e盘里面生成的a.html成果为:



    原来,再大的房子,再大的床,没有相爱的人陪伴,都只是冰冷的物质。而如果身边有爱人陪伴,即使房子小,床小,也觉得无关紧要,因为这些物质上面有了爱的温度,成了家的元素。—— 何珞《婚房》#书摘#
    分享到: