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>
================================