Spring mvc 3.0 怎么配置velocity tools (dateTool/numberTool)?

使用Spring mvc3.0 的时候,希望在velocity模板页面格式化输出时间,于是想到velocity可以配置一些工具(tools)在VM中使用,但是却不知道怎么在 spring mvc 中配置,差了些资料,看了些代码,测试成功,写出来给自己留个底,也给大家参考。

 

 

只需要在应用的servlet配置文件中(XXX-servlet.xml )的viewResolver bean添加属性如下:

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. <!-- view resolver -->   
  2. < bean   id = "viewResolver"   
  3.     class = "org.springframework.web.servlet.view.velocity.VelocityViewResolver" >   
  4.     < property   name = "prefix"   value = ""   />   
  5.     < property   name = "suffix"   value = ".vm"   />   
  6.     < property   name = "contentType"   value = "text/html;charset=UTF-8"   />   
  7.     < property   name = "exposeRequestAttributes"   value = "true"   />   
  8.     < property   name = "exposeSpringMacroHelpers"   value = "true"   />   
  9.     < property   name = "dateToolAttribute" >   
  10.         < value > dateTool </ value >   
  11.     </ property >   
  12.     < property   name = "attributes" >   
  13.         < props >   
  14.             < prop   key = "demandsServerUrl" > ${demands.web.server.url} </ prop >   
  15.             < prop   key = "htdocsBaseUrl" > ${demands.web.htdocs.baseUrl} </ prop >   
  16.         </ props >   
  17.     </ property >   
  18. </ bean >   

这样就可以在页面上如下使用:

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. $dateTool.format( "yyyy-MM-dd" ,$!{demand.gmtCreate})  

另外,也可以定义一些在页面上公用的变量,如上面xml中的demandsServerUrl htdocsBaseUrl

你可能感兴趣的:(spring mvc)