freemarker 自定义标签 博客分类: Freemarker freemarkerDAOIOCJavaEXT
程序员文章站
2024-03-08 14:11:40
...
最近项目中要将前台的好多首页静态化,当然首选freemarker,可前台首页算下来有好十几个,这样一个一个在页面用freemarker标签不累死人。在网上找了好多资料,发现freemarker可以像jsp那样自定义标签。嘿嘿 这样就简单了将要显示的内容给他输出出来,页面控制他的样式,这不就省了不少事
将自定义freemarker 标签添加到ExtendServlet
ExtendServlet继承FreemarkerServlet[代码]
在web.xml添加相应配置
[代码] 使用自己的标签
<@label id=21>
</@label>
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>
[代码] 使用自己的标签
<@label id=21>
</@label>
上一篇: 用compass快速给你的网站添加搜索功能<二> 博客分类: Compass Lucene log4jJ#BeanSpringfreemarker
下一篇: SOA and Web services @ IBM DeveloperWorks 博客分类: web service IBMWebSOA
推荐阅读
-
freemarker 自定义标签 博客分类: Freemarker freemarkerDAOIOCJavaEXT
-
FreeMarker整合Spring 3 博客分类: Freemarker SpringfreemarkerJSPWebAOP
-
freemarker属性文件配置详解 博客分类: Freemarker freemarker
-
freemarker中实现自定义标签(包含处理参数以及循环变量) 博客分类: Freemarker freemarkerJava
-
freemarker属性文件配置详解 博客分类: Freemarker freemarker
-
myEclipse 8.6安装freemarker插件 博客分类: Freemarker MyeclipsefreemarkerIDE.net
-
freemarker中实现自定义标签(包含处理参数以及循环变量) 博客分类: Freemarker freemarkerJava
-
一步一步教你写自己的java自定义标签 博客分类: JavaWeb javaweb自定义标签java标签
-
FreeMarker整合Spring 3 博客分类: Freemarker SpringfreemarkerJSPWebAOP
-
spring boot 集成 shiro 自定义密码验证 自定义freemarker标签根据权限渲染不同页面(推荐