webwork *.action的执行流程(http://seadragonnj.javaeye.com/blog/144412)

这里把webwork的工作流程作一个简单的记录吧,算是这里看源码的总结:

当一个*.action请求过来之后 ,被com.opensymphony.webwork.dispatcher.ServletDispatcher这个类截获,很自然这个Servlet首先会调用

java 代码
  1. public void init(ServletConfig servletConfig) throws ServletException {   
  2.       super.init(servletConfig);   
  3.       DispatcherUtils.initialize(getServletContext());   
  4.   }  
这个方法先进行一些初始化工作,它最终调用DispatcherUtils的init()方法做初始化工作,init()方法通过调用default.properties这样的属性文件进行初始化,初始化工作结束后调用
java 代码
  1. public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException   

这个方法,剩下所有事情都发生到这里了~~~~

java 代码
  1. public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {   
  2.        DispatcherUtils du = DispatcherUtils.getInstance();   
  3.        //根据前面的初始数据对字符集、本地化等一些基本信息进行设置   
  4.        du.prepare(request, response);   
  5.        //(一)、通过request对象,读取URL来得到action的name和methodName   
  6.        ActionMapping mapping = ActionMapperFactory.getMapper().getMapping(request);   
  7.        if (mapping == null) {   
  8.            try {   
  9.                response.sendError(404);   
  10.            } catch (IOException e) {   
  11.                LOG.error("Could not send 404 after not finding any ActionMapping", e);   
  12.            }   
  13.            return;   
  14.        }   
  15.   
  16.        try {   
  17.         //对HttpServletRequest进行了包装   
  18.            request = du.wrapRequest(request, getServletContext());   
  19.        } catch (IOException e) {   
  20.            String message = "Could not wrap servlet request with MultipartRequestWrapper!";   
  21.            LOG.error(message, e);   
  22.            throw new ServletException(message, e);   
  23.        }   
  24.        //转发继续处理   
  25.        du.serviceAction(request, response, getServletContext(), mapping);   
  26.    }  

在这里有必要看一下上面的(一)发生了什么

首先通过工厂类来得到一个ActionMaper的实例,ActionMaper就是提供一个request到action的mapping(映射),它其实做很少的工作,它并不会去考虑这个映射的合法性。工厂类通过读取default.properties属性文件里的

  1. webwork.mapper.class=com.opensymphony.webwork.dispatcher.mapper.DefaultActionMapper  

 得到要实例化那个对象,这个对象会根据URL得到nameSpace,actionnName,methodNamet等信息。最后转发到DispatcherUtils对象的serviceAction(request, response, getServletContext(), mapping);方法继续处理。

在这个方法中做的主要工作就是创建一个DefaultActionProxy对象,它是ActionProxy默认实现,它就是一个从webwork到xwork的action之间的一个桥梁

DefaultActionProxy的构建函数中,有通过

  1. config = ConfigurationManager.getConfiguration().getRuntimeConfiguration().getActionConfig(namespace, actionName);//这里完成解析xwork.xml的工作  

 这段代码解析xwork.xml文件,实际上它最后通过XmlConfigurationProvider这个类进行XML文件的解析,在DefaultActionProxy这个对象的构建过程中还会创建一个DefaultActionInvocation对象,它是ActionInvocation的默认实现,action执行就是在这里完成的,包括所有interptors和action执行后的result的执行,都在这里完成,这个类的关键是invoke()这个方法,

java 代码
  1.  public String invoke() throws Exception {   
  2.         if (executed) {   
  3.             throw new IllegalStateException("Action has already executed");   
  4.         }   
  5.         //执行interceptors,这里用的责任链模式,最好看一下这个模式,才能对这段代码怎么运行有一个更好的理解   
  6.         if (interceptors.hasNext()) {   
  7.             Interceptor interceptor = (Interceptor) interceptors.next();   
  8.             resultCode = interceptor.intercept(this);   
  9.         } else {   
  10.             //在action之后调用Action类中的方法,并返回result代码   
  11.             resultCode = invokeActionOnly();   
  12.         }   
  13.   
  14.         // this is needed because the result will be executed, then control will return to the Interceptor, which will   
  15.         // return above and flow through again   
  16. //      这里干什么不太清楚   
  17.         if (!executed) {   
  18.             if (preResultListeners != null) {   
  19.                 for (Iterator iterator = preResultListeners.iterator();   
  20.                      iterator.hasNext();) {   
  21.                     PreResultListener listener = (PreResultListener) iterator.next();   
  22.                     listener.beforeResult(this, resultCode);   
  23.                 }   
  24.             }   
  25. //            在这里执行result   
  26.             if (proxy.getExecuteResult()) {   
  27.                 executeResult();   
  28.             }   
  29.   
  30.             executed = true;   
  31.         }   
  32.   
  33.         return resultCode;   
  34.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值