velocity的开发指南 (八)

2014-11-24 11:27:38 · 作者: · 浏览: 53
*/
EventCartridge ec = new EventCartridge();

/*
* then register this class as it contains the handlers
*/
ec.addEventHandler(this);

/*
* and then finally let it attach itself to the context
*/
ec.attachToContext( context );

/*
* now merge your template with the context as you normally
* do
*/

....
}

/*
* and now the implementations of the event handlers
*/
public Object referenceInsert( String reference, Object value )
{
/* do something with it */
return value;
}

public boolean shouldLogOnNullSet( String lhs, String rhs )
{
if ( /* whatever rule */ )
return false;

return true;
}

public Object methodException( Class claz, String method, Exception e )
throws Exception
{
if ( /* whatever rule */ )
return "I should have thrown";

throw e;
}
}


10.Velocity Configuration Keys and Values(配置参数名字和值说明)
Velocity's runtime configuration is controlled by a set of configuration keys listed below Velocity的运行时配置由一个key-value列表控制.
Velocity中有一些默认的值在以下位置可以找到:
/src/java/org/apache/velocity/runtime/defaults/velocity.defaults,Velocity基础配置,它会确保Velocity总是有一个“正常的”配置以便运行.但它不一定是你想要的配置.
任何配置必须在 init()调用前发生才会在运行时替换掉默认的配置。这意味着你只需改变你需要的而不是所有的配置都要动.
这一节: Using Velocity In General Applications 有关于configuration API的进一步说明.
以下,是默认配置的说明:
1.Runtime Log
runtime.log = velocity.log 用以指定Velocity运行时日志文件的路劲和日志文件名,如不是全限定的绝对路径,系统会认为想对于当前目录.
runtime.log.logsystem 这个参数没有默认值,它可指定一个实现了interface org.apache.velocity.runtime.log.LogSystem.的自定义日志处理对象给Velocity。这就方便将Velocity与你己有系统的日志机制统一起来.具体可见Configuring the Log System 这一节.
runtime.log.logsystem.class = org.apache.velocity.runtime.log.AvalonLogSystem 上面这行,是一个示例来指定一个日志记录器.
runtime.log.error.stacktrace = false runtime.log.warn.stacktrace = false runtime.log.info.stacktrace = false 这些是错误消息跟踪的开关.将会生成大量、详细的日志内容输出.
runtime.log.invalid.references = true 当一个引用无效时,打开日志输出. 在生产系统运行中,这很有效,也是很有用的调试工具.
2.字符集编码问题
input.encoding = ISO-8859-1 输出模板的编码方式 (templates). 你可选择对你模板的编码方式,如UTF-8.GBK.
output.encoding = ISO-8859-1 VelocityServlet 对输出流(output streams)的编码方式.
3.#foreach() Directive
directive.foreach.counter.name = velocityCount 在模板中使用#foreach() 指令时,这里设定的字符串名字将做为context key 代表循环中的计数器名,如以上设定,在模板中可以通过 $velocityCount来访问.
directive.foreach.counter.initial.value = 1 #foreach() 中计数器的起始值.
4.#include() and #parse() Directive
directive.include.output.errormsg.start = directive.include.output.errormsg.end = 使用#include()时,定义内部流中开始和结束的错误消息标记,如果两者都设这屯,错误消息将被输出到流中'.但必须是两都定义.
directive.parse.maxdepth = 10 定义模板的解析深度,当在一个模板中用#parse()指示解析另外一个模板时,这个值可以防止解析时出现recursion解析.
5.资源管理
resource.manager.logwhenfound = true 定义日志中的 'found' 务目开关.当打开时,如Resource Manager第一次发现某个资源时, the first time, the resource name and classname of the loader that found it will be noted in the runtime log.
resource.loader = (default = File) Multi-valued key. Will accept CSV for value. (可以有多个以.号分开的值),可以理解为指定资源文件的扩展名.
.loader.description = Velocity File Resource Loader 描述资源装载器名字.
.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader 实现的资源装载器的类名. 默认的是文件装载器.
.resource.loa