Webwork基本原理
概要分析一下流程
-
Web.xml
webwork
com.opensymphony.webwork.dispatcher.FilterDispatcher
webwork
/*
com.opensymphony.webwork.dispatcher.FilterDispatcher,从这里开始了webwork2之旅[by gaoling]
-
FilterDispatcher.doFilter
-
将Request.parameter->map中
ActionMapping mapping = mapper.getMapping(request);
-
du.serviceAction(request, response, servletContext, mapping);
将控制权交给DispatcherUtils
-
DispatcherUtils
-
这里定义了ActionProxy
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);
-
然后调用了proxy.execute();
下面到了xwork部分
-
com.opensymphony.xwork.DefaultActionProxy[by gaoling]
retCode = invocation.invoke();
// invocation:com.opensymphony.xwork.DefaultActionInvocation
-
com.opensymphony.xwork.DefaultActionInvocation
看看invocation.invoke();方法
if (interceptors.hasNext()) {//执行拦截器
InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
//会递归到invocation.invoke();下面我们看看是如何实现递归的。
//关键在下面一句话,interceptor.getInterceptor()会返回一个com.opensymphony.xwork.interceptor.Interceptor,Interceptor.intercept(invocation)方法调用了result = invocation.invoke();[by gaoling]
resultCode = interceptor.getInterceptor().intercept(this);
} else {
//当所有的拦截器执行完毕后,执行invokeActionOnly
resultCode = invokeActionOnly();
}
-
invokeActionOnly
invokeActionOnly这个函数会反射到我们的action中,执行我们的业务逻辑代码
return (String) method.invoke(action, new Object[0]);
-
执行完业务逻辑代码,回到上面的递归
再看invocation.invoke()方法:
if (!executed) {
if (preResultListeners != null) {
for (Iterator iterator = preResultListeners.iterator();
iterator.hasNext();) {
PreResultListener listener = (PreResultListener) iterator.next();
listener.beforeResult(this, resultCode);
}
}
// now execute the result, if we're supposed to
if (proxy.getExecuteResult()) {
executeResult();//返回result
}
executed = true;
}
-
executeResult();
result.execute(this);
Represents a generic interface for all action execution results, whether that be displaying a webpage, generating an email, sending a JMS message, etc.
#上面只是很粗浅的分析,下一步看看webwork2是如何处理request,action,配置映射,result等细节问题的。