当前位置: 代码迷 >> Web前端 >> Struts2+Spring+Freemarker兑现自定义标签
  详细解决方案

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

热度:798   发布时间:2012-08-24 10:00:21.0
Struts2+Spring+Freemarker实现自定义标签

?1、实现自定义FreemarkerManager

?

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

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

4、在spring上下文中配置

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

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

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

?

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