首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Struts2+Spring+Freemarker兑现自定义标签

2012-08-24 
Struts2+Spring+Freemarker实现自定义标签?1、实现自定义FreemarkerManager?import?freemarker.template.Co

Struts2+Spring+Freemarker实现自定义标签

?1、实现自定义FreemarkerManager

?

  1. import?freemarker.template.Configuration;?import?freemarker.template.TemplateDirectiveModel;?
  2. import?freemarker.template.TemplateException;?import?org.apache.struts2.views.freemarker.FreemarkerManager;?
  3. import?org.springframework.context.ApplicationContext;?import?org.springframework.web.context.support.WebApplicationContextUtils;?import?javax.servlet.ServletContext;?
  4. import?java.util.Map;?
  5. /**?
  6. ?*?@desc:?增加自定义业务标签,此list中的string必须继承于TemplateDirectiveModel,并且配置在SPRING上下文中??*/?
  7. public?class?CustomFreemarkerManager?extends?FreemarkerManager?{?
  8. ????protected?Configuration?createConfiguration(ServletContext?servletContext)?throws?TemplateException?{?????????Configuration?cfg?=?super.createConfiguration(servletContext);?
  9. ???????ApplicationContext?ctx?=?WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);????????Map<String,Object>?beans=(Map<String,Object>)ctx.getBeansOfType(TemplateDirectiveModel.class);?
  10. ????????for?(String?key?:?beans.keySet())?{?????????????Object?bean?=?beans.get(key);?
  11. ????????????if?(bean?!=?null?&&?bean?instanceof?TemplateDirectiveModel)?????????????????cfg.setSharedVariable(key,?bean);?
  12. ????????}?????????return?cfg;?
  13. ????}?}?

?2、在struts.properties中配置你的自定义管理类

  1. struts.freemarker.manager.classname=com.kms.framework.core.view.support.KmsFreemarkerManager?

?3、自定义TemplateDirectiveModel

  1. import?static?freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;????
  2. import?java.io.IOException;??import?java.util.HashMap;??
  3. import?java.util.Iterator;??import?java.util.List;??
  4. import?java.util.Map;??import?java.util.Map.Entry;??
  5. ??import?javax.servlet.http.HttpSession;??
  6. ??import?org.apache.struts2.ServletActionContext;??
  7. ??import?com.newyulong.bcp.product.domain.TypeInfo;??
  8. import?com.newyulong.bcp.product.service.ITypeService;??import?com.newyulong.bcp.user.vo.Loginer;??
  9. ??import?freemarker.core.Environment;??
  10. import?freemarker.template.TemplateDirectiveBody;??import?freemarker.template.TemplateDirectiveModel;??
  11. import?freemarker.template.TemplateException;??import?freemarker.template.TemplateModel;??
  12. import?freemarker.template.TemplateModelException;??import?freemarker.template.TemplateNumberModel;??
  13. ??/**??
  14. ??*?@desc:?产品类别自定义标签??
  15. ?*/??public?class?ProductTypeTemplateDirectiveModel?implements?TemplateDirectiveModel?{??
  16. ????//产品分类服务?????private?ITypeService?typeService;??
  17. ????//?展示产品类别数目??????private?static?final?String?PARAM_COUNT?=?"count";??
  18. ???
  19. ????//?默认??????private?static?final?int?DEAFAULT_COUNT?=?5;??
  20. ????????public?void?execute(Environment?environment,?Map?map,??
  21. ????????????TemplateModel[]?templateModels,?TemplateDirectiveBody?body)??????????????throws?TemplateException,?IOException?{??
  22. ????????int?count?=?0,?hot?=?0;??????????boolean?countParamSet?=?false;??
  23. ????????//?获取传递进来的参数??????????Iterator?it?=?map.entrySet().iterator();??
  24. ????????while?(it.hasNext())?{??????????????Map.Entry?entry?=?(Entry)?it.next();??
  25. ????????????String?paramName?=?entry.getKey().toString();??????????????TemplateModel?paramValue?=?(TemplateModel)?entry.getValue();??
  26. ????????????if?(paramName.equals(PARAM_COUNT))?{??????????????????if?(!(paramValue?instanceof?TemplateNumberModel))?{??
  27. ????????????????????throw?new?TemplateModelException("The?""?+?PARAM_COUNT??????????????????????????????+?""?parameter?"?+?"must?be?a?number.");??
  28. ????????????????}??????????????????count?=?((TemplateNumberModel)?paramValue).getAsNumber()??
  29. ????????????????????????.intValue();??????????????????countParamSet?=?true;??
  30. ????????????????if?(count?<?0)?{??????????????????????throw?new?TemplateModelException("The?""?+?PARAM_COUNT??
  31. ????????????????????????????+?""?parameter?"?+?"can't?be?negative.");??????????????????}??
  32. ????????????}?else?{??????????????????throw?new?TemplateModelException("Unsupported?parameter:?"??
  33. ????????????????????????+?paramName);??????????????}??
  34. ??????????}??
  35. ????????//?参数为空时候设置默认值??????????if?(!countParamSet)??
  36. ????????????count?=?DEAFAULT_COUNT;???????????????????Map<String,?Object>?maps?=?getTypeList(String.valueOf(count));??
  37. ??????????if?(body?!=?null)?{??
  38. ??????????????environment.setVariable("typelist",??
  39. ????????????????????DEFAULT_WRAPPER.wrap(maps.get("typeList")));??????????????//?真正开始处理输出内容??
  40. ????????????body.render(environment.getOut());??????????}?else?{??
  41. ????????????throw?new?RuntimeException("missing?body");??????????}??
  42. ????}????
  43. ????/**???????*???
  44. ?????*?@function?产品类别查询列表???????*?@param?count??
  45. ?????*?@param?hot???????*?@return??
  46. ?????*????????*/??
  47. ????public?Map<String,?Object>?getTypeList(String?count)?{??????????Map<String,?Object>?maps?=?new?HashMap<String,?Object>();??
  48. ????????//?1.获取产品列表??????????List<TypeInfo>?list?=?typeService.getPreTypeList(count);??
  49. ????????maps.put("typeList",?list);??????????return?maps;??
  50. ??????}??
  51. ????//?获得用户信息?????private?Loginer?getLoginer()?{??
  52. ????????HttpSession?session?=?ServletActionContext.getRequest().getSession();??????????return?(Loginer)?session.getAttribute(Loginer.USER_IN_SESSION);??
  53. ????}????
  54. ????public?void?setTypeService(ITypeService?typeService)?{??????????this.typeService?=?typeService;??
  55. ????}??

4、在spring上下文中配置

  1. <!--产品类别标签-->??<bean?id="ptype"?class="com.newyulong.bcp.web.tag.ProductTypeTemplateDirectiveModel"></bean>??

?5、在页面中使用你的标签吧

  1. <div?class="gen">??<h2?class="gen_tit">产品分类</h2>??
  2. ????<ul>????????<!--产品分类-->??
  3. ??????<@ptype>???????????????????<#list?list?as?entity>??
  4. ?????????????????????<li?class="bto">????????????????????????????<a?href="${ctx}/web/productt.do?type=${entity.id}">??
  5. ?????????????????????????????<#if?entity.title?length?gt?12?>???????????????????????????????????${entity.title[0..11]}...??
  6. ??????????????????????????????<#else>???????????????????????????????????${entity.title}??
  7. ?????????????????????????????</#if>???????????????????????????</a>??
  8. ?????????????????????</li>???????????????????</#list>??
  9. ?????????</@ptype>??????</ul>??
  10. </div>??

?

1 楼 dl374868471 2012-04-23   最近在做个需要生成静态页的 项目 大虾方便的话 能否帮小弟指教一下QQ:374868471 多谢

热点排行