spring中velocity的配置
1velocity.properties文件的配置
runtime.log.invalid.reference = true
input.encoding=UTF-8
output.encoding=UTF-8
----------------------------------------------------------
directive.foreach.counter.name = velocityCount
directive.foreach.counter.initial.value = 1
directive.include.output.errormsg.start = <!-- include error :
directive.include.output.errormsg.end = see error log -->
# ----------------------------------------------------------------------------
# P A R S E P R O P E R T I E S
# ----------------------------------------------------------------------------
directive.parse.max.depth = 10
# ----------------------------------------------------------------------------
# VELOCIMACRO PROPERTIES
# ----------------------------------------------------------------------------
# global : name of default global library. It is expected to be in the regular
# template path. You may remove it (either the file or this property) if
# you wish with no harm.
# ----------------------------------------------------------------------------
# dev-changes by Marino
webapp.resource.loader.cache = true
webapp.resource.loader.modificationCheckInterval = 5
velocimacro.library.autoreload = false
velocimacro.library = /WEB-INF/VM_global_library.vm
velocimacro.permissions.allow.inline = true
velocimacro.permissions.allow.inline.to.replace.global = false
velocimacro.permissions.allow.inline.local.scope = false
velocimacro.context.localscope = false
# ----------------------------------------------------------------------------
# INTERPOLATION
# ----------------------------------------------------------------------------
# turn off and on interpolation of references and directives in string
# literals. ON by default :)
# ----------------------------------------------------------------------------
runtime.interpolate.string.literals = true
# ----------------------------------------------------------------------------
# RESOURCE MANAGEMENT
# ----------------------------------------------------------------------------
# Allows alternative ResourceManager and ResourceCache implementations
# to be plugged in.
# ----------------------------------------------------------------------------
resource.manager.class = org.apache.velocity.runtime.resource.ResourceManagerImpl
resource.manager.cache.class = org.apache.velocity.runtime.resource.ResourceCacheImpl
# ----------------------------------------------------------------------------
# user common
# ----------------------------------------------------------------------------
userdirective=
着重注意的是velocimacro.library,其实路径为项目的根目录,这个文件主要配置的是被项目全局使用的volocity宏。
2 spring中的配置
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="/" />
<property name="configLocation">
//加载上面的配置文件
<value>/WEB-INF/velocity.properties</value>
</property>
</bean>
3 示例
VM_global_library.vm拥有宏
#macro(hello)
hello
#end
单个hello.vm文件为
渲染hello.vm文件#hello()
张三
HashMap data = new HashMap();
String s = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,template, encode, data);
System.out.println(s);
输出结果为
hello
张三