Velocity在渲染页面的时候,提供了不同的EventHanlder,供开发者callback。
本文简要说明下,在Velocity1.6.1版本下,不同EventHanlder的作用:
EventHandler(接口):
Base interface for all event handlers
仅仅是一个事件侦听标志
IncludeEventHandler(接口):
Event handler for include type directives (e.g.
#include()
,
#parse()
)
Allows the developer to modify the path of the resource returned.
在使用#include(),#parse()语法的时候,允许开发修改include或者parse文件的路径(一般用于资源找不到的情况)
IncludeNotFound(IncludeEventHandler实现类):
Simple event handler that checks to see if an included page is available.
If not, it includes a designated replacement page instead.By default, the name of the replacement page is "notfound.vm", however this
page name can be changed by setting the Velocity property
eventhandler.include.notfound
, for example:
eventhandler.include.notfound = error.vm
当使用#include(),#parse()语法的时候,如果提供的资源文件找不到,则默认使用notfound.vm模板代替。
开发者可以通过设置eventhandler.include.notfound
属性,修改替代模板的路径。
IncludeRelativePath(IncludeEventHandler实现类):
Event handler that looks for included files relative to the path of the
current template. The handler assumes that paths are separated by a forward
slash "/" or backwards slash "\".
使用相对路径方式,寻找#include或者#parse()中指定的资源文件
InvalidReferenceEventHandler(接口):
Event handler called when an invalid reference is encountered. Allows
the application to report errors or substitute return values
当渲染页面的时候,一旦遇到非法的reference,就会触发此事件。开发者可以侦听此事件,用于错误的报告,或者修改返回的内容。
ReportInvalidReferences(InvalidReferenceEventHandler实现类):
Use this event handler to flag invalid references.
使用这个实现类用于标志非法的references。修改eventhandler.invalidreference.exception属性,可以在捕捉到第一个非法references的时候,停止模板的渲染。
MethodExceptionEventHandler(接口):
Event handler called when a method throws an exception. This gives the
application a chance to deal with it and either
return something nice, or throw.
Please return what you want rendered into the output stream.
渲染模板,一旦发现调用的方法抛出异常的时候,就会触发此事件。允许开发者处理这个异常,输出友好信息或者抛出异常。必须返回一个值用于模板的渲染。
PrintExceptions(MethodExceptionEventHandler实现类):
Simple event handler that renders method exceptions in the page
rather than throwing the exception. Useful for debugging.
By default this event handler renders the exception name only.
To include both the exception name and the message, set the property
eventhandler.methodexception.message
to true
. To render
the stack trace, set the property eventhandler.methodexception.stacktrace
to true
.
模板渲染时,遇到方法异常,输出异常名,而不是抛出这个异常。对于调式,非常有帮助。
通过eventhandler.methodexception.message
和eventhandler.methodexception.stacktrace
属性的设置,可以输出异常message和stacktrace.
NullSetEventHandler(接口):
Event handler called when the RHS of #set is null. Lets an app approve / veto
writing a log message based on the specific reference.
当使用#set()语法,设置一个null值的时候,会触发此事件。--目前Velocity官方没有提供默认实现。
ReferenceInsertionEventHandler(接口):
Reference 'Stream insertion' event handler. Called with object
that will be inserted into stream via value.toString().
Please return an Object that will toString() nicely
当渲染变量(reference)的时候,就会触发此事件。允许开发者返回更加友好的值--一般用于内容的escape,比如HtmlEscape等。
EscapeHtmlReference(ReferenceInsertionEventHandler实现类):
html escape
EscapeJavaScriptReference(ReferenceInsertionEventHandler实现类):
javascript escape
EscapeSqlReference(ReferenceInsertionEventHandler实现类):
sql escape
EscapeXmlReference(ReferenceInsertionEventHandler实现类):
xml escape
以上是Velocity组件中提供的EventHandler介绍。下面写一个简单的例子来说明EventHandler的使用。
模拟需求,假如输出的内容带有html标签,而输出的内容需要过滤这些标签。如果我们手工对输出变量通过StringEscapeUtils.escapeHtml()来实现,则太过繁琐。所以,我们就可以使用Velocity中的EscapeHtmlReference。demo代码如下:
VelocityEngine ve = new VelocityEngine();
EventCartridge eventCartridge = new EventCartridge();
eventCartridge.addEventHandler(new EscapeHtmlReference());
Context context = new VelocityContext();
context.put("name", "<table></table>");
eventCartridge.attachToContext(context);
StringWriter writer = new StringWriter();
ve.mergeTemplate(VM_LOCATION, "utf-8", context, writer);
System.out.println("================================");
System.out.println(writer.toString());
System.out.println("================================");
模板文件中,仅仅为 $name
则输出内容如下:
================================
<table></table>
================================