<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 閱讀(2628) 評論(0)  編輯  收藏 所屬分類: java

    主站蜘蛛池模板: 色综合久久精品亚洲国产| 亚洲欧洲日韩国产综合在线二区| 免费一本色道久久一区| 国产高清免费观看| 亚洲日韩精品一区二区三区| 亚洲精品综合一二三区在线| 国产精品网站在线观看免费传媒| 成人影片麻豆国产影片免费观看 | 亚洲同性男gay网站在线观看| 亚洲JLZZJLZZ少妇| 日本视频在线观看永久免费| 免费看大美女大黄大色| 亚洲高清国产AV拍精品青青草原| 亚洲中文字幕日本无线码| 青柠影视在线观看免费| 巨胸喷奶水视频www网免费| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 一级白嫩美女毛片免费| 亚州免费一级毛片| 国产福利电影一区二区三区,亚洲国模精品一区 | 国产亚洲综合成人91精品| 在线观看免费亚洲| 一个人看的www在线免费视频| 国产中文在线亚洲精品官网| 亚洲日韩国产二区无码| ww4545四虎永久免费地址| 色老板亚洲视频免在线观| 国产乱弄免费视频| 天天综合亚洲色在线精品| 在线观看午夜亚洲一区| 美女内射无套日韩免费播放| 久久亚洲国产午夜精品理论片| 91老湿机福利免费体验| 亚洲精品制服丝袜四区| 67pao强力打造国产免费| 久久精品国产亚洲av麻豆色欲| 两性色午夜视频免费网| 亚洲午夜国产片在线观看| 久久成人免费大片| 亚洲AV无码男人的天堂 | 亚洲国产综合在线|