velocity配置详解
在velocity二方包中有一个velocity.properties
文件定义了velocity的配置信息(位于org.apache.velocity.runtime.defaults package下,org.apache.velocity.runtime.RuntimeConstants中定义了key值)
#模板编码:
input.encoding=ISO-8859-1 //模板输入编码
output.encoding=ISO-8859-1 //模板输出编码
#foreach配置
directive.foreach.counter.name = velocityCount //计数器名称
directive.foreach.counter.initial.value = 1 //计数器初始值
directive.foreach.maxloops = -1 //最大循环次数,-1为默认不限制 directive.foreach.iterator.name = velocityHasNex //迭代器名称
#set配置
directive.set.null.allowed = false //是否可设置空值
#include配置
directive.include.output.errormsg.start = <!– include error : //错误信息提示开始字符串
directive.include.output.errormsg.end = see error log –> //错误信息提示结束字符串
#parse配置
directive.parse.max.depth = 10 //解析深度
模板加载器配置
resource.loader = file //模板加载器类型,默认为文件,可定义多个
file.resource.loader.description = Velocity File Resource Loader //加载器描述
file.resource.loader.class = Velocity.Runtime.Resource.Loader.FileResourceLoader //加载器类名称
file.resource.loader.path = . //模板路径
file.resource.loader.cache = false //是否启用模板缓存
file.resource.loader.modificationCheckInterval = 2 //检查模板更改时间间隔
宏配置
velocimacro.permissions.allow.inline = true //是否可以行内定义
velocimacro.permissions.allow.inline.to.replace.global = false //是否可以用行内定义代替全局定义
velocimacro.permissions.allow.inline.local.scope = false //行内定义是否只用于局部
velocimacro.context.localscope = false //宏上下文是否只用于局部
velocimacro.max.depth = 20 //解析深度
velocimacro.arguments.strict = false //宏参数是否启用严格模式
资源管理器配置
resource.manager.class = Velocity.Runtime.Resource.ResourceManagerImpl //管理器类名称
resource.manager.cache.class = Velocity.Runtime.Resource.ResourceCacheImpl //缓存器类名称
解析器池配置
parser.pool.class = Velocity.Runtime.ParserPoolImpl //解析池类名称
parser.pool.size = 40 //初始大小
#evaluate配置
directive.evaluate.context.class = Velocity.VelocityContext //上下问类名称
可插入introspector配置
runtime.introspector.uberspect = Velocity.Util.Introspection.UberspectImpl //默认introspector类名称
更详细的可以参考:Velocity的控制讨论(http://hi.baidu.com/maomaoyusp/item/980a650836c29c7fbee97e22)
directive.foreach.counter.name
directive.foreach.counter.name属性用于指定VTL标识符,用于#foreach指令的循环计数器名称。当使用了前缀$时,这个标识符用作Velocity变量引用,它允许模板设计者访问#foreach指令的当前反复数(即循环数)。默认情况下,这个计数器从1开始,且每一次循环的增量为1。directive. foreach.counter.name属性的值默认为velocityCount,其相应的变量引用为$velocityCount。如果模板设计者想用引用名称$my-Count来代替这个值,则可以为该属性赋值为myCount。
directive.foreach.counter.initial.value
directive.foreach.counter.initial.value属性指定了#foreach指令的循环计数器初始值,这个值通过循环计数器引用提供(见directive.foreach.counter.name属性),作为每一个后来反复的起点,这个值将增加一个步长(At the beginning of each subsequent iteration, the value is incremented by one)。directive.foreach.counter.initial.value的默认值是1。熟悉的C++和Java loop循环的模板设计者或许更喜欢基于0的计数器,那么你把这个属性设置为0即可。
directive.include.output.errormsg.start
directive.include.output.errormsg.start属性指定了在(由非法输入参数被传递到#include指令而产生的)错误信息之前的文本。比如错误输入了一个未定义或不明确的(undefined)Velocity引用,将触发这种错误信息。这个错误信息前缀通过定义这个属性来指定。其默认值为“<!– include error :”。
directive.include.output.errormsg.end
directive.include.output.errormsg.end属性指定了跟随在(由非法输入参数被传递到#include指令而产生的)错误信息之后的文本。比如错误输入了一个未定义或不明确的(undefined)Velocity引用,将触发这种错误信息。这个错误信息后缀通过定义这个属性来指定。其默认值为“see error log –>”。
directive.parse.max.depth
directive.parse.max.depth属性指定了#parse指令可以嵌套的最大深度。当值为1时,从本质上已经禁用了#parse指令,当一个模板包含了#parse指令时,深度的值就已经为1了,所以该值至少为1。虽然该属性的主要目的是为了防止递归失控,但该深度限度也适用于普通不含递归的#parse嵌套。其默认值为10。
Encoding
下面的属性用于指定模板和数据的编码(The following properties specify encodings to be associated with templates and data used by certain tools associated with the Velocity template engine)。
input.encoding
input.encoding属性被用于指定模板引擎进行模板处理的编码。一旦设定好后,所有的模板输入都将转换为指定的编码。默认值为ISO-8859-1。所支持的编码依赖于Java字符集。
output.encoding
output.encoding属性被用于指定输出流的编码。这个不是多种用途的Velocity属性,通常情况下,仅供VelocityServlet类和Anakia项目使用。普通情况下,在Writer被合并之前,编码可以在Writer类实例的初始化时直接指定。其默认值为ISO-8859-1。
Logging
下面的属性将影响Velocity日志系统的行为。
runtime.log
runtime.log属性用于指定Velocity日志文件的路径。默认情况下,这个路径被指定为应用程序的相对路径,这个设置可以通过file.resource.loader.path属性(下面将要讨论的)定义来修改。runtime.log属性的默认值是velocity.log。通过前面的示例,对这个文件你应该比较熟悉了,每一次运行Velocity应用程序里,就会产生这个文件。
runtime.log.logsystem
runtime.log.logsystem属性定义了一个Velocity将要传递(hand off/手传手?)日志任务的对象,用于Velocity。为了使用这个对象,你需要有一个实现org.apache.velocity.runtime. log.LogSystem接口的类。这个属性主要用于当Velocity的日志需要与定制应用程序日志类结合的情况下。该属性没有默认值,同时要注意,该属性值最好是一个对象(当然也可以用字符串),它不可以被直接指定为配置文件或Java属性对象。
runtime.log.logsystem.class
runtime.log.logsystem.class属性用于指定运行时实例化Velocity日志服务的类。该属性的值可以由逗号分隔的类名称列表组成。运行时引擎按列表里的名称次序依次查找匹配的类。第一个匹配的类将用于Velocity日志的实例化。默认值为“org.apache.velocity. runtime.log.AvalonLogSystem, org.apache.velocity.runtime.log.SimpleLog4J logSystem”。日志功能也可以通过把该属性设置为org.apache.velocity. runtime.log.NullLogSystem而禁用。
runtime.log.error.stacktrace
runtime.log.error.stacktrace属性用于指定当Velocity运行时引擎记录错误日志时,是否允许产生和日志堆栈跟踪信息(既是否产生堆栈的跟踪日志信息)。虽然它自己支持这个功能,但其关联的功能仍旧没有实现(Although support for the property itself exists, the associated functionality is not yet implemented)。默认值为false。
runtime.log.warn.stacktrace
runtime.log.warn.stacktrace属性用于指定当Velocity运行时引擎日志一个警告时,是否允许产生和日志堆栈跟踪信息。虽然它自己支持这个功能,但其关联的功能仍旧没有实现(Although support for the property itself exists, the associated functionality is not yet implemented)。默认值为false。
runtime.log.info.stacktrace
runtime.log.info.stacktrace属性用于指定当Velocity运行时引擎日志一个报告信息时,是否允许产生和日志堆栈跟踪信息。虽然它自己支持这个功能,但其关联的功能仍旧没有实现(Although support for the property itself exists, the associated functionality is not yet implemented)。默认值为false。
runtime.log.invalid.references
runtime.log.invalid.references属性用于指定是否要对模板里的非法Velocity引用错误进行日志。如果值为true,非法引用将产生一个警告信息。如果值为false,将忽略非法引用的信息。默认值为true。
链接: http://fcjblog.com/the-java-velocity-velocity-and-configuration-in-detail