Webwork基本原理

来源:互联网 发布:duffle coat 知乎 编辑:程序博客网 时间:2024/04/28 11:40

Webwork基本原理

概要分析一下流程

 

  1. Web.xml

webwork

com.opensymphony.webwork.dispatcher.FilterDispatcher

 

webwork

/*

 

com.opensymphony.webwork.dispatcher.FilterDispatcher,从这里开始了webwork2之旅[by gaoling]

 

 

  1. FilterDispatcher.doFilter
  • Request.parameter->map

ActionMapping mapping = mapper.getMapping(request);

  • du.serviceAction(request, response, servletContext, mapping);

将控制权交给DispatcherUtils

 

  1. DispatcherUtils
  • 这里定义了ActionProxy

ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);

  • 然后调用了proxy.execute();

 

下面到了xwork部分

 

  1. com.opensymphony.xwork.DefaultActionProxy[by gaoling]

retCode = invocation.invoke();

// invocation:com.opensymphony.xwork.DefaultActionInvocation

 

 

  1. com.opensymphony.xwork.DefaultActionInvocation

看看invocation.invoke();方法

 

if (interceptors.hasNext()) {//执行拦截器

InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();

            //递归到invocation.invoke();下面我们看看是如何实现递归的。
//关键在下面一句话,interceptor.getInterceptor()会返回一个com.opensymphony.xwork.interceptor.Interceptor,Interceptor.interceptinvocation)方法调用了result = invocation.invoke();[by gaoling]

resultCode = interceptor.getInterceptor().intercept(this);

} else {

            //当所有的拦截器执行完毕后,执行invokeActionOnly

resultCode = invokeActionOnly();

}

 

  1. invokeActionOnly

invokeActionOnly这个函数会反射到我们的action中,执行我们的业务逻辑代码

return (String) method.invoke(action, new Object[0]);

 

 

  1. 执行完业务逻辑代码,回到上面的递归

再看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;

}

 

  1. 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等细节问题的。

 

 

原创粉丝点击