首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Xwork2 源码翻阅(二)

2012-07-28 
Xwork2 源码阅读(二)Dispatcher类Dispatcher类是在struts2中定义的,在 webwork2 中叫DispatcherUtils,?作

Xwork2 源码阅读(二)

Dispatcher类

Dispatcher类是在struts2中定义的,在 webwork2 中叫DispatcherUtils,

?

作用相同:

1 初始化模块参数,

2 将 xwork 于 web层分离,

3 中转请求,进入xwork 的 action处理流程。

?

1 初始化,涉及方法 init()

在上一篇的 FilterDispatcher中的init() 方法,有这么两句:

?

?

                        public?String?<SPAN?style="COLOR:?#ff0000">invokeActionOnly()</SPAN>?throws?Exception?{ ??????return?invokeAction(getAction(),?proxy.getConfig()); ??} ????rotected?String?invokeAction(Object?action,?ActionConfig?actionConfig)?throws?Exception?{ ??????String?methodName?=?proxy.getMethod(); ????????if?(LOG.isDebugEnabled())?{ ??????????LOG.debug("Executing?action?method?=?"?+?actionConfig.getMethodName()); ??????} ????????String?timerKey?=?"invokeAction:?"+proxy.getActionName(); ??????try?{ ??????????UtilTimerStack.push(timerKey); ?????????? ??????????boolean?methodCalled?=?false; ??????????Object?methodResult?=?null; ??????????Method?method?=?null; ??????????try?{ ??????????????<SPAN?style="COLOR:?#ff0000">method?=?getAction().getClass().getMethod(methodName,?new?Class[0]);</SPAN> ??????????}?catch?(NoSuchMethodException?e)?{ ??????????????//?hmm?--?OK,?try?doXxx?instead ??????????????try?{ ??????????????????String?altMethodName?=?"do"?+?methodName.substring(0,?1).toUpperCase()?+?methodName.substring(1); ??????????????????method?=?getAction().getClass().getMethod(altMethodName,?new?Class[0]); ??????????????}?catch?(NoSuchMethodException?e1)?{ ??????????????????//?well,?give?the?unknown?handler?a?shot ??????????????????if?(unknownHandler?!=?null)?{ ??????????????????try?{ ??????????????????????methodResult?=?unknownHandler.handleUnknownActionMethod(action,?methodName); ??????????????????????methodCalled?=?true; ??????????????????}?catch?(NoSuchMethodException?e2)?{ ??????????????????????//?throw?the?original?one ??????????????????????throw?e; ??????????????????} ??????????????????}?else?{ ??????????????????throw?e; ??????????????} ??????????????} ??????????} ?????????? ??????????if?(!methodCalled)?{ ??????????????<SPAN?style="COLOR:?#ff0000">methodResult?=?method.invoke(action,?new?Object[0]);</SPAN> ??????????} ?????????? ??????????if?(methodResult?instanceof?Result)?{ ??????????????this.explicitResult?=?(Result)?methodResult; ??????????????return?null; ??????????}?else?{ ??????????????return?(String)?methodResult; ??????????} ??????}?catch?(NoSuchMethodException?e)?{ ??????????throw?new?IllegalArgumentException("The?"?+?methodName?+?"()?is?not?defined?in?action?"?+?getAction().getClass()?+?""); ??????}?catch?(InvocationTargetException?e)?{ ??????????//?We?try?to?return?the?source?exception. ??????????Throwable?t?=?e.getTargetException(); ????????????if?(actionEventListener?!=?null)?{ ??????????????String?result?=?actionEventListener.handleException(t,?getStack()); ??????????????if?(result?!=?null)?{ ??????????????????return?result; ??????????????} ??????????} ??????????if?(t?instanceof?Exception)?{ ??????????????throw(Exception)?t; ??????????}?else?{ ??????????????throw?e; ??????????} ??????}?finally?{ ??????????UtilTimerStack.pop(timerKey); ??????} ??}??
                      [java] view plaincopy
                      1. public?String?<span?style="color:?#ff0000;">invokeActionOnly()</span>?throws?Exception?{??????return?invokeAction(getAction(),?proxy.getConfig());??
                      2. }????
                      3. rotected?String?invokeAction(Object?action,?ActionConfig?actionConfig)?throws?Exception?{??????String?methodName?=?proxy.getMethod();??
                      4. ??????if?(LOG.isDebugEnabled())?{??
                      5. ????????LOG.debug("Executing?action?method?=?"?+?actionConfig.getMethodName());??????}??
                      6. ??????String?timerKey?=?"invokeAction:?"+proxy.getActionName();??
                      7. ????try?{??????????UtilTimerStack.push(timerKey);??
                      8. ??????????????????boolean?methodCalled?=?false;??
                      9. ????????Object?methodResult?=?null;??????????Method?method?=?null;??
                      10. ????????try?{??????????????<span?style="color:?#ff0000;">method?=?getAction().getClass().getMethod(methodName,?new?Class[0]);</span>??
                      11. ????????}?catch?(NoSuchMethodException?e)?{??????????????//?hmm?--?OK,?try?doXxx?instead??
                      12. ????????????try?{??????????????????String?altMethodName?=?"do"?+?methodName.substring(0,?1).toUpperCase()?+?methodName.substring(1);??
                      13. ????????????????method?=?getAction().getClass().getMethod(altMethodName,?new?Class[0]);??????????????}?catch?(NoSuchMethodException?e1)?{??
                      14. ????????????????//?well,?give?the?unknown?handler?a?shot??????????????????if?(unknownHandler?!=?null)?{??
                      15. ????????????????try?{??????????????????????methodResult?=?unknownHandler.handleUnknownActionMethod(action,?methodName);??
                      16. ????????????????????methodCalled?=?true;??????????????????}?catch?(NoSuchMethodException?e2)?{??
                      17. ????????????????????//?throw?the?original?one??????????????????????throw?e;??
                      18. ????????????????}??????????????????}?else?{??
                      19. ????????????????throw?e;??????????????}??
                      20. ????????????}??????????}??
                      21. ??????????????????if?(!methodCalled)?{??
                      22. ????????????<span?style="color:?#ff0000;">methodResult?=?method.invoke(action,?new?Object[0]);</span>??????????}??
                      23. ??????????????????if?(methodResult?instanceof?Result)?{??
                      24. ????????????this.explicitResult?=?(Result)?methodResult;??????????????return?null;??
                      25. ????????}?else?{??????????????return?(String)?methodResult;??
                      26. ????????}??????}?catch?(NoSuchMethodException?e)?{??
                      27. ????????throw?new?IllegalArgumentException("The?"?+?methodName?+?"()?is?not?defined?in?action?"?+?getAction().getClass()?+?"");??????}?catch?(InvocationTargetException?e)?{??
                      28. ????????//?We?try?to?return?the?source?exception.??????????Throwable?t?=?e.getTargetException();??
                      29. ??????????if?(actionEventListener?!=?null)?{??
                      30. ????????????String?result?=?actionEventListener.handleException(t,?getStack());??????????????if?(result?!=?null)?{??
                      31. ????????????????return?result;??????????????}??
                      32. ????????}??????????if?(t?instanceof?Exception)?{??
                      33. ????????????throw(Exception)?t;??????????}?else?{??
                      34. ????????????throw?e;??????????}??
                      35. ????}?finally?{??????????UtilTimerStack.pop(timerKey);??
                      36. ????}??}??

                      ?

                      method = getAction().getClass().getMethod(methodName, new Class[0]);取出需执行的action的方法,如 execute()

                      ?

                      methodResult = method.invoke(action, new Object[0]);利用反射,执行action 方法

                      ?

                      上边就是利用proxy模式,调用一个action的具体逻辑 的 过程。

                      ?

                      一般的action里,都有一些变量,如从request中取出的参数等,

                      如要执行action 方法,如execute(),则必须先吧这些变量赋值 已供方法中使用,

                      这个工作,就是 拦截器(Interceptor) 的任务之一,

                      ?

                      下篇探讨一下拦截器。

热点排行