大 蘋 果

          獻給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

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 从江县| 潼关县| 新和县| 永川市| 承德市| 方正县| 谢通门县| 黑水县| 砀山县| 泽库县| 曲阜市| 丁青县| 宕昌县| 宁河县| 黑龙江省| 黄浦区| 义乌市| 泰来县| 博湖县| 沙田区| 蓬安县| 湖北省| 潮州市| 泰来县| 天气| 和平县| 庄河市| 和田市| 盈江县| 芜湖县| 江达县| 莆田市| 西贡区| 应城市| 五家渠市| 溧水县| 漳浦县| 乐山市| 桐梓县| 静海县| 鄂伦春自治旗|