freemarker 自定义标签

最近项目中要将前台的好多首页静态化,当然首选freemarker,可前台首页算下来有好十几个,这样一个一个在页面用freemarker标签不累死人。在网上找了好多资料,发现freemarker可以像jsp那样自定义标签。嘿嘿 这样就简单了将要显示的内容给他输出出来,页面控制他的样式,这不就省了不少事 

package com.f139.frame.freemarker.directive;



import java.io.IOException;

import java.io.Writer;

import java.util.Map;



import org.nutz.dao.Dao;



import com.f139.frame.pojo.factory.Label;



import freemarker.core.Environment;

import freemarker.template.TemplateDirectiveBody;

import freemarker.template.TemplateDirectiveModel;

import freemarker.template.TemplateException;

import freemarker.template.TemplateModel;



public class LabelDirective implements TemplateDirectiveModel {



    private Dao dao;



    public LabelDirective(Dao dao) {

        this.dao = dao;

    }



    @Override

    @SuppressWarnings("unchecked")

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

        

        for (TemplateModel templateModel : loopVars) {

            templateModel.toString();

        }

        

        // 真正开始处理输出内容

        Writer out = env.getOut();



        if (body != null) {

            out.write(getContent(params));

            body.render(out);

        }

    }



    private String getContent(Map<?,?> params) {

        Integer id = Integer.parseInt(params.get("id").toString());

        if (null != id) {

            Label label = this.dao.fetch(Label.class, id);

            return label.getLabelContent();

        }

        return null;

    }

}

将自定义freemarker 标签添加到ExtendServlet 

package com.f139.frame.servlet;



import org.nutz.dao.Dao;

import org.nutz.dao.impl.NutDao;

import org.nutz.ioc.Ioc;

import org.nutz.ioc.impl.NutIoc;

import org.nutz.ioc.loader.json.JsonLoader;



import com.f139.frame.tag.RepeatDirective;



import freemarker.ext.jsp.TaglibFactory;

import freemarker.ext.servlet.FreemarkerServlet;

import freemarker.template.Configuration;

import freemarker.template.TemplateModel;

import freemarker.template.TemplateModelException;





public class ExtendServlet extends FreemarkerServlet {



    /**

     * 

     */

    private static final long serialVersionUID = 1L;



    private Dao dao;



    public ExtendServlet() {

        Ioc ioc = new NutIoc(new JsonLoader("com/f139/frame/ioc"));

        this.dao = ioc.get(NutDao.class, "dao");

    }



    @Override

    protected Configuration createConfiguration() {

        //添加自定义标签

        Configuration cfg = super.createConfiguration();

        cfg.setSharedVariable("label", new LabelDirective(dao));

        return cfg;

    }



}

ExtendServlet继承FreemarkerServlet[代码] 
在web.xml添加相应配置 

 1 <servlet>

 2         <servlet-name>freemarker</servlet-name>

 3         <servlet-class>com.f139.frame.servlet.ExtendServlet</servlet-class>

 4         <init-param>

 5             <param-name>TemplatePath</param-name>

 6             <param-value>/</param-value>

 7         </init-param>

 8         <init-param>

 9             <param-name>NoCache</param-name>

10             <param-value>true</param-value>

11         </init-param>

12         <init-param>

13             <param-name>ContentType</param-name>

14             <param-value>text/html;charset=UTF-8</param-value>

15         </init-param>

16         <init-param>

17             <param-name>template_update_delay</param-name>

18             <param-value>0</param-value>

19         </init-param>

20         <init-param>

21             <param-name>default_encoding</param-name>

22             <param-value>UTF-8</param-value>

23         </init-param>

24         <init-param>

25             <param-name>number_format</param-name>

26             <param-value>0.##########</param-value>

27         </init-param>

28         <load-on-startup>1</load-on-startup>

29     </servlet>

[代码] 使用自己的标签 
<@label id=21> 

</@label>

你可能感兴趣的:(freemarker)