在使用vm做为视图展示时,Spring框架本身提供了一些定义好的宏让开发者重用.如在spring.vm文件中,如页面表单绑定常用的宏:
而在实际项目中,开发者要定义很多自己的宏以使一些功能得到重用.如何让spring加载自定的宏文件呢.在以下配置中:
该属性configLocation设置了velocity属性配置文件的路径及文件名,.要想使用自定的宏,只需在velocity.properties文件中指定如下两项的值.
#macro( springBind $path )
#if("$!springHtmlEscape" != "")
#set( $status = $springMacroRequestContext.getBindStatus($path, $springHtmlEscape) )
#else
#set( $status = $springMacroRequestContext.getBindStatus($path) )
#end
#end
而在实际项目中,开发者要定义很多自己的宏以使一些功能得到重用.如何让spring加载自定的宏文件呢.在以下配置中:
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
<value>/WEB-INF/vm/</value>
</property>
<property name="configLocation" value="/WEB-INF/velocity.properties" />
</bean>
该属性configLocation设置了velocity属性配置文件的路径及文件名,.要想使用自定的宏,只需在velocity.properties文件中指定如下两项的值.
velocimacro.library = macros-default.vm
velocimacro.library.autoreload = true
而在这个文件macros-default.vm中,我们自定义的一些宏都可以放入其中.