<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    stone2083

    Velocity之EventHandler

    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.messageeventhandler.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

    則輸出內容如下:

    ================================
    &lt;table&gt;&lt;/table&gt;
    ================================

    posted on 2009-02-05 22:26 stone2083 閱讀(2625) 評論(0)  編輯  收藏 所屬分類: java

    主站蜘蛛池模板: 中文字幕亚洲码在线| 亚洲人成电影在线观看网| 自拍偷自拍亚洲精品偷一| 毛片a级毛片免费播放100| 自拍日韩亚洲一区在线| 大陆一级毛片免费视频观看i| 亚洲香蕉在线观看| 国产福利在线免费| 亚洲AV综合色区无码一二三区| 成在人线AV无码免费| 亚洲AV色无码乱码在线观看| 免费日韩在线视频| 国产免费久久精品99久久| 亚洲精品国产美女久久久| 午夜精品免费在线观看 | 国产亚洲午夜高清国产拍精品| 无码日韩人妻AV一区免费l| 久久精品夜色噜噜亚洲A∨| 国产一区二区三区免费| 亚洲综合免费视频| 毛片视频免费观看| 十八禁的黄污污免费网站| 综合亚洲伊人午夜网| 999久久久免费精品播放| 亚洲第一成人在线| 亚洲不卡无码av中文字幕| 国产一区二区免费| 久久国产亚洲精品| 亚洲av无码国产综合专区 | 在线视频免费观看爽爽爽| 亚洲国产精品99久久久久久| 亚洲一区日韩高清中文字幕亚洲| 久久国产乱子伦精品免费一| 亚洲日韩中文字幕无码一区| 久久久亚洲精品蜜桃臀| 中文字幕无码播放免费| 美女隐私免费视频看| 精品亚洲国产成AV人片传媒| 国产成人精品免费视频大全五级| 国产成人免费ā片在线观看老同学 | 亚洲日本va在线观看|