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

    主站蜘蛛池模板: 中文字幕a∨在线乱码免费看 | 3d动漫精品啪啪一区二区免费| 国产在线19禁免费观看| 亚洲成AV人片在WWW| 国产一级一片免费播放| 国产精品亚洲一区二区无码| 国产青草视频在线观看免费影院| 亚洲av无码专区在线电影天堂| 免费精品一区二区三区在线观看| 亚洲AV成人无码网站| 免费一区二区三区四区五区 | 中国人免费观看高清在线观看二区| 亚洲片一区二区三区| 日本三级在线观看免费| 久久亚洲国产午夜精品理论片| 国产99视频精品免费专区| 亚洲视频小说图片| 午夜高清免费在线观看| 美女黄频视频大全免费的| 国产成人精品亚洲精品| 一级毛片免费毛片一级毛片免费| 亚洲日韩乱码中文无码蜜桃臀| 一个人免费观看视频www| 美女视频黄a视频全免费网站一区| 亚洲女同成人AⅤ人片在线观看| a级片免费观看视频| 亚洲第一页在线视频| 美女被免费视频网站a国产 | 国产亚洲精品AA片在线观看不加载 | 美女免费视频一区二区| 亚洲人成色777777在线观看| 1000部无遮挡拍拍拍免费视频观看| 亚洲资源最新版在线观看| 亚洲AV无码不卡在线观看下载| a级大片免费观看| 中文字幕亚洲码在线| 区久久AAA片69亚洲| 一级女人18毛片免费| 国产精品内射视频免费| 亚洲精品午夜国产va久久| 国产成人亚洲精品狼色在线|