springmvc+velocity 在vm模板上添加通用工具类对象变量

在平时工作的项目中经常会在vm模板中添加自定义的工具类,由于上班时用的是sofa框架,里面的配置跟spring还是有区别的,以前自己也没有做过,今天就尝试了一下,主要配置如下:

Xml代码   收藏代码
  1. <!-- 让Spring启用对annotation的支持 -->   
  2.     < context:annotation-config > </ context:annotation-config >   
  3.   
  4. <!-- 自动扫描org.yonge路径下的所有文件,并根据注解完成注入的工作 -->   
  5.     < context:component-scan   base-package = "com.yonge" > </ context:component-scan >   
  6.   
  7.     <!-- 配置velocity引擎 -->   
  8.     < bean   id = "velocityConfigurer"   
  9.         class = "org.springframework.web.servlet.view.velocity.VelocityConfigurer" >   
  10.         < property   name = "resourceLoaderPath"   value = "/"   /> <!-- 模板存放的路径 -->   
  11.         < property   name = "velocityProperties" >   
  12.             < props >   
  13.                 < prop   key = "input.encoding" > gbk </ prop >   
  14.                 < prop   key = "output.encoding" > gbk </ prop >   
  15.             </ props >   
  16.         </ property >   
  17.     </ bean >   
  18.   
  19.     <!-- 配置视图的显示 -->   
  20.     < bean   id = "ViewResolver"   
  21.         class = "org.springframework.web.servlet.view.velocity.VelocityViewResolver" >   
  22.         < property   name = "prefix"   value = "/"   /> <!-- 视图文件的前缀,即存放的路径 -->   
  23.         < property   name = "suffix"   value = ".vm"   /> <!-- 视图文件的后缀名 -->   
  24.         < property   name = "exposeRequestAttributes"   value = "true"   /> <!-- 暴露request属性 -->   
  25.         < property   name = "toolboxConfigLocation"   value = "/WEB-INF/velocity/tools.xml"   /> <!-- 配置工具类 -->   
  26.         <!-- VelocityToolboxView 类只支持1.X的velocitytools -->   
  27.         <!--< property   name = "viewClass"   
  28.             value = "org.springframework.web.servlet.view.velocity.VelocityToolboxView"   />  -- >   
  29.         < property   name = "viewClass"   
  30.             value = "com.yonge.web.velocity.VelocityToolbox20View"   />   
  31.         <!-- < property   name = "viewClass"   
  32.             value = "org.springframework.web.servlet.view.velocity.VelocityView"   />  -- >   
  33.     </ bean >   
  34.   
  35.     <!-- 将自定义工具类 注入到VelocityContext中-->   
  36.     <!-- < bean   name = "velocityView"   
  37.         class = "org.springframework.web.servlet.view.velocity.VelocityView" >   
  38.         < property   name = "toolAttributes" >   
  39.             < map >   
  40.                 < entry   key = "dateUtil"   value = "com.yonge.web.util.DateUtil"   />   
  41.             </ map >   
  42.         </ property >   
  43.         < property   name = "url"   value = "/"   />   
  44.     </ bean >  -- >   

 

上面的配置文件中,VelocityToolbox20View是重写的一个类,是在网上copy的,如下:

Java代码   收藏代码
  1. public   class  VelocityToolbox20View  extends  VelocityToolboxView {  
  2.     @Override   
  3.     protected  Context createVelocityContext(Map<String, Object> model,  
  4.             HttpServletRequest request, HttpServletResponse response)  
  5.             throws  Exception { // Create a   
  6.                                 // ChainedContext   
  7.                                 // instance.   
  8.         ViewToolContext ctx;  
  9.   
  10.         ctx = new  ViewToolContext(getVelocityEngine(), request, response,  
  11.                 getServletContext());  
  12.   
  13.         ctx.putAll(model);  
  14.   
  15.         if  ( this .getToolboxConfigLocation() !=  null ) {  
  16.             ToolManager tm = new  ToolManager();  
  17.             tm.setVelocityEngine(getVelocityEngine());  
  18.             tm.configure(getServletContext().getRealPath(  
  19.                     getToolboxConfigLocation()));  
  20.             if  (tm.getToolboxFactory().hasTools(Scope.REQUEST)) {  
  21.                 ctx.addToolbox(tm.getToolboxFactory().createToolbox(  
  22.                         Scope.REQUEST));  
  23.             }  
  24.             if  (tm.getToolboxFactory().hasTools(Scope.APPLICATION)) {  
  25.                 ctx.addToolbox(tm.getToolboxFactory().createToolbox(  
  26.                         Scope.APPLICATION));  
  27.             }  
  28.             if  (tm.getToolboxFactory().hasTools(Scope.SESSION)) {  
  29.                 ctx.addToolbox(tm.getToolboxFactory().createToolbox(  
  30.                         Scope.SESSION));  
  31.             }  
  32.         }  
  33.         return  ctx;  
  34.     }  
  35. }  

 velocityTool的配置文件:

Xml代码   收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2.   
  3. < tools >   
  4.     < data   type = "boolean"   key = "xhtml"   value = "true" />   
  5.     < data   type = "boolean"   key = "isSimple"   value = "true" />   
  6.     < data   type = "number"   key = "version"   value = "2.0" />   
  7.     < data   key = "foo" > this is foo </ data >   
  8.     < data   key = "bar" > this is bar. </ data >   
  9.       
  10.     < toolbox   scope = "request" >   
  11.         <!-- <tool key="toytool" class="ToyTool" restrictTo="index*"/> -->   
  12.     </ toolbox >   
  13.       
  14.     < toolbox   scope = "session" >   
  15.         <!-- <tool key="map" class="java.util.HashMap"/> -->   
  16.     </ toolbox >   
  17.       
  18.     < toolbox   scope = "application" >   
  19.         <!-- <tool key="map" class="java.util.HashMap"/> -->   
  20.         < tool   key = "dateUtil"   class = "com.yonge.web.util.DateUtil" />   
  21.     </ toolbox >   
  22. </ tools >   

 用velocityTool很好的解决了添加自定义工具类的问题,但是我最开始的时候是想通过另一种VelocityView解决这个问题,向 velocityView中注入toolAttributes属性(如上面的配置文件),该属性的类型是一个Map<String, Class>,key是vm模板上使用的变量名,value是对应自定义的工具类,它也会将toolAttributes属性添加到 velocityContext中,但是我却遇到了velocityView不能注入viewClass的问题,因为velocityView是一个实例 对象,而viewClass是一个class类型的,导致类型转换错误,希望知道的兄弟们回复一下,thx!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值