Xwork2 源码翻阅(二)
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- 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);??
- ????}??}??
?
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) 的任务之一,
?
下篇探讨一下拦截器。