大 蘋 果

          獻給Eclipse社區

           

          Velocity的事件處理

           

          Velocity提供了對模板解析過程事件的處理,用戶可以響應模板產生的事件。模板事件有設置模板變量、模板參數為空以及調用模板方法時出錯等。

          要響應模板事件可以實現Velocity的接口,提供相應的處理。模板事件都繼承于“org.apache.velocity.app.event.EventHandler”接口,有如下幾個接口的實現:

          1. ReferenceInsertionEventHandler:要實現的方法為referenceInsert,響應從模板上下文給模板變量賦值。

                 2. NullSetEventHandler:要實現的方法為shouldLogOnNullSet,響應當賦值的變量為空的事件。

          3. MethodExceptionEventHandler:要實現的方法為methodException,響應當調用的方法拋出異常時的處理。

          模板的事件提供了用戶代碼和模板引擎的交互的方式,為演示三種類型的模板事件,可以通過實例進行介紹,如例程9-10所示。

          例程9-10 模板事件的處理

          public class EventHandler implements ReferenceInsertionEventHandler,

                             NullSetEventHandler, MethodExceptionEventHandler {

           

                   public EventHandler(Context ctx) {

                             EventCartridge ec = new EventCartridge();

           

                             //添加模板事件的處理實例

                             ec.addEventHandler(this);

                             //添加模板上下文

                             ec.attachToContext(ctx);

                   }

           

                   /**

                    * 處理引用值的插件

                    * */

                   public Object referenceInsert(String reference, Object data) {

                             System.out.println("referenceInsert: " + reference + " data: " + data);

           

                             return data;

                   }

           

                   /**

                    * 處理空值

                    * @param lhs 被賦值變量

                    * @param rhs 將要賦的值

                    * */

                   public boolean shouldLogOnNullSet(String lhs, String rhs) {

                             System.out.println("Null:");

                             System.out.println("lhs:" + lhs + " rhs:" + rhs);

           

                             return true;

                   }

           

                   /**

                    * 處理模板調用方法拋出的異常

                    * @param cls 拋出異常的類

                    * @param method 拋出異常的方法

                    * @param e 拋出的異常

                    * */

                   public Object methodException(Class cls, String method, Exception e)

                                      throws Exception {

                             return "An " + e.getClass().getName() + " was thrown by the " + method

                                               + " method of the " + cls.getName() + " class ["

                                               + e.getMessage() + "]";

                   }

           

                   public static void main(String[] args) throws Exception {

                             Velocity.init();

                             Template t = Velocity.getTemplate("./src/eventHandler.vm");

           

                             Context ctx = new VelocityContext();

                             ctx.put("person", "Joe");

                             ctx.put("exception", new ExceptionGenerator());

           

                             //設置異常處理類及上下文

                             EventHandler hdl = new EventHandler(ctx);

           

                             Writer writer = new StringWriter();

                             t.merge(ctx, writer);

           

                             System.out.println(writer);

                   }

           

          }

           

           

          public class ExceptionGenerator {

           

           public String generate() throws Exception {

              Random rnd = new Random();

           

              int x = rnd.nextInt(5);

           

              if (x == 2) {

                throw new Exception("Unlucky!");

              } else {

                return "No Exception";

              }

           }

          }

           

          模板文件eventHandler.vm

          #set($myNull1 = $testNull)

           

          This is $person

           

          $exception.generate()

          如上實例,通過ExceptionGenerator來生成異常,測試異常事件的監聽,當ExceptionGenerator拋出異常時

          posted on 2007-04-08 23:53 阿甘 閱讀(1133) 評論(1)  編輯  收藏

          評論

          # re: Velocity的事件處理 2007-06-26 20:39 bearhunter

          學習了  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(10)

          隨筆分類

          隨筆檔案(2)

          相冊

          funny

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 临洮县| 姚安县| 龙陵县| 文山县| 馆陶县| 辉县市| 麻阳| 雷山县| 泽普县| 金溪县| 奉化市| 白沙| 阜康市| 根河市| 勃利县| 云梦县| 西昌市| 青州市| 大安市| 余干县| 鹰潭市| 太保市| 万源市| 蚌埠市| 正镶白旗| 寿宁县| 商洛市| 铁力市| 武山县| 湖口县| 板桥市| 永兴县| 科技| 分宜县| 汉川市| 徐汇区| 邹平县| 南岸区| 东丽区| 汉沽区| 神农架林区|