Velocity配置详解

 转自:http://blog.csdn.net/zengdeqing2012/article/details/51140239

 一 springmvc中velocity配置详解

1.1 web.xml配置

[html]  view plain  copy
  1. <context-param>  
  2.         <param-name>contextConfigLocation</param-name>  
  3.         <param-value>  
  4.             classpath*:common/common.xml  
  5.         </param-value>  
  6.     </context-param>  
注: common.xml可以随便取名,知道是velocity的配置就行。

1.2 common.xml (velocity.xml的配置)

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans default-autowire="byName">  
  4.     <!-- 配置velocity引擎 -->   
  5.     <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">  
  6.         <property name="resourceLoaderPath">  
  7.             <value>/WEB-INF/templates</value>  <!-- 模板存放的路径 -->   
  8.         </property>  
  9.         <property name="configLocation">  
  10.             <value>classpath:common/velocity.properties</value>  
  11.         </property>  
  12.         <property name="velocityProperties">  
  13.             <props>  
  14.                 <prop key="directive.foreach.counter.name">loopCounter</prop>  
  15.                 <prop key="directive.foreach.counter.initial.value">0</prop>  
  16.                 <prop key="directive.foreach.iterator.name">loopHasNext</prop>  
  17.             </props>  
  18.         </property>  
  19.     </bean>  
  20.       
  21.     <!--配置视图的显示:配置附加工具,以及将后缀为vm的文件交给下面的Resolver处理-->  
  22.     <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">  
  23.         <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/> <!--toolbox配置文件路径-->    
  24.         <property name="prefix" value="/"/> <!-- 视图文件的前缀,即存放的路径 -->   
  25.         <property name="suffix" value=".vm" /> <!-- 视图文件的后缀名 -->  
  26.         <property name="layoutUrl" value="layout/layout.vm" /> <!--指定layout文件-->  
  27.         <property name="contentType" value="text/html;charset=UTF-8" /> <!--指定编码-->  
  28.         <property name="exposeSpringMacroHelpers" value="true" /> <!--是否使用spring对宏定义的支持-->  
  29.         <property name="layoutKey" value="layout"></property>    
  30.         <property name="screenContentKey" value="screen_content" />  
  31.           
  32.         <property name="dateToolAttribute">  
  33.             <value>dateTool</value> <!--日期函数名称-->  
  34.         </property>  
  35.         <property name="numberToolAttribute">  
  36.             <value>numberTool</value> <!--数字函数名称-->  
  37.         </property>  
  38.           
  39.         <property name="allowRequestOverride" value="true" />  
  40.         <property name="exposeRequestAttributes" value="true" /> <!--是否开放request属性-->  
  41.         <property name="exposeSessionAttributes" value="true" /> <!--是否开放session属性-->  
  42.         <property name="requestContextAttribute" value="rc"/> <!--request属性引用名称-->  
  43.           
  44.     </bean>  
  45. </beans>  

注: 配置layoutUrl设定系统默认的模板路径
      layoutKey设定模板文件键值,设定该值后就可以在vm文件中使用该键值设置模板路径,
      screenContentKey表示指定vm文件显示位置

1.3 velocity.properties配置

[html]  view plain  copy
  1. #模板编码:  
  2. input.encoding=ISO-8859-1 //模板输入编码  
  3. output.encoding=ISO-8859-1 //模板输出编码  
  4.   
  5. #foreach配置  
  6. directive.foreach.counter.name = velocityCount //计数器名称  
  7. directive.foreach.counter.initial.value = 1 //计数器初始值  
  8. directive.foreach.maxloops = -1 //最大循环次数,-1为默认不限制 directive.foreach.iterator.name = velocityHasNex //迭代器名称  
  9.   
  10. #set配置  
  11. directive.set.null.allowed = false //是否可设置空值  
  12.   
  13. #include配置  
  14. directive.include.output.errormsg.start = <!-- include error : //错误信息提示开始字符串  
  15. directive.include.output.errormsg.end = see error log --> //错误信息提示结束字符串  
  16.   
  17. #parse配置  
  18. directive.parse.max.depth = 10 //解析深度  
  19.   
  20. 模板加载器配置  
  21. resource.loader = file //模板加载器类型,默认为文件,可定义多个  
  22. file.resource.loader.description = Velocity File Resource Loader //加载器描述  
  23. file.resource.loader.class = Velocity.Runtime.Resource.Loader.FileResourceLoader //加载器类名称  
  24. file.resource.loader.path = . //模板路径  
  25. file.resource.loader.cache = false //是否启用模板缓存  
  26. file.resource.loader.modificationCheckInterval = 2 //检查模板更改时间间隔  
  27.   
  28. 宏配置  
  29. velocimacro.permissions.allow.inline = true //是否可以行内定义  
  30. velocimacro.permissions.allow.inline.to.replace.global = false //是否可以用行内定义代替全局定义  
  31. velocimacro.permissions.allow.inline.local.scope = false //行内定义是否只用于局部  
  32. velocimacro.context.localscope = false //宏上下文是否只用于局部  
  33. velocimacro.max.depth = 20 //解析深度  
  34. velocimacro.arguments.strict = false //宏参数是否启用严格模式  
  35.   
  36. 资源管理器配置  
  37. resource.manager.class = Velocity.Runtime.Resource.ResourceManagerImpl //管理器类名称  
  38. resource.manager.cache.class = Velocity.Runtime.Resource.ResourceCacheImpl //缓存器类名称  
  39.   
  40. 解析器池配置  
  41. parser.pool.class = Velocity.Runtime.ParserPoolImpl //解析池类名称  
  42. parser.pool.size = 40 //初始大小  
  43.   
  44. #evaluate配置  
  45. directive.evaluate.context.class = Velocity.VelocityContext //上下问类名称  
  46.   
  47. 可插入introspector配置  
  48. runtime.introspector.uberspect = Velocity.Util.Introspection.UberspectImpl //默认introspector类名称  

如:我们项目中使用的(根据实际情况,配置几项即可)
[html]  view plain  copy
  1. tools.view.servlet.error.template = Error.vm  
  2.   
  3. input.encoding=UTF-8  
  4.    
  5. output.encoding=UTF-8  
  6.   
  7. runtime.log = velocity.log   
  8.   
  9. velocimacro.library=VM_global_library.vm  
  10.   
  11. runtime.log.invalid.references = true  
  12.   
  13. file.resource.loader.cache=false  
  14.   
  15. velocimacro.library.autoreload = true  


1.4 toolbox.xml配置

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <toolbox>  
  4.   <tool>  
  5.     <key>date</key>  
  6.     <scope>request</scope>  
  7.     <class>  
  8.       org.apache.velocity.tools.generic.DateTool  
  9.     </class>  
  10.     <parameter name="format" value="yyyy-MM-dd HH:mm:ss"/>  
  11.   </tool>  
  12.   <tool>  
  13.     <key>link</key>  
  14.     <scope>request</scope>  
  15.     <class>org.apache.velocity.tools.view.tools.LinkTool</class>  
  16.   </tool>  
  17.   <tool>  
  18.     <key>stringUtils</key>  
  19.     <scope>request</scope>  
  20.     <class>org.apache.velocity.util.StringUtils</class>  
  21.   </tool>  
  22.   <tool>  
  23.     <key>math</key>  
  24.     <scope>application</scope>  
  25.     <class>org.apache.velocity.tools.generic.MathTool</class>  
  26.   </tool>  
  27.   <tool>  
  28.     <key>esc</key>  
  29.     <scope>request</scope>  
  30.     <class>org.apache.velocity.tools.generic.EscapeTool</class>  
  31.   </tool>  
  32.   <tool>  
  33.     <key>params</key>  
  34.     <scope>request</scope>  
  35.     <class>org.apache.velocity.tools.view.tools.ParameterParser</class>  
  36.   </tool>  
  37. </toolbox>  
注:如果配置了toolbox.xml则在velocity模板中可直接使用java后台代码了。

1.5 layout.vm布局文件配置

 路径:根据common.xml配置,此时我的位置为: /WEB-INF/templates/layout/layout.vm
[html]  view plain  copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8">  
  5.     <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />  
  6.     <title id="title1">$!page_title</title>  
  7.     #parse("/layout/commonCss.vm") <!-- 公用css -->  
  8.     #parse("/layout/commonJs.vm")  <!-- 公用js -->  
  9. </head>  
  10. <body>  
  11.     $screen_content  
  12. </body>  
  13. </html>  

1.6 default.vm布局文件配置

位置为: /WEB-INF/templates/layout/default.vm
[html]  view plain  copy
  1. $screen_content  

1.7 使用范例

1.7.1 普通页面

[html]  view plain  copy
  1. #set($page_title="普通页面")  
  2. <div>  
  3.     <p>显示页面内容,好处:加载默认样式,样式统一控制</p>  
  4. </div>  
注:因为头部与尾部都是统一控制的,所以只需显示中间的内容即可。


1.7.2 自定义页面

[html]  view plain  copy
  1. #set($page_title="自定义样式页面")  
  2. #set($layout="/layout/default.vm")  
  3. <div>  
  4.     <p>设置某页面不使用 velocity配置默认的layout.vm模板,好处:不会加载默认样式,可以自定义</p>  
  5. </div>  

总结:
通过以上配置后普通页面velocity会自动套用layout/layout.vm模板
如果登录页面需套用自己独特的模板则如下
如:
可以在登录页面中添加:
[html]  view plain  copy
  1. #set($layout="/layout/default.vm")  
则登录页面将套用"default.vm"模板,样式需自定义;尤其是样式冲突时,非常好用 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值