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添加相应配置

<servlet>
		<servlet-name>freemarker</servlet-name>
		<servlet-class>com.f139.frame.servlet.ExtendServlet</servlet-class>
		<init-param>
			<param-name>TemplatePath</param-name>
			<param-value>/</param-value>
		</init-param>
		<init-param>
			<param-name>NoCache</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>ContentType</param-name>
			<param-value>text/html;charset=UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>template_update_delay</param-name>
			<param-value>0</param-value>
		</init-param>
		<init-param>
			<param-name>default_encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>number_format</param-name>
			<param-value>0.##########</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

[代码]使用自己的标签

<@labelid=21>

</@label>

相关推荐