WebWork的执行流程

开发 后端
本文介绍WebWork的工作流程。

 一、WebWork的框架初始化过程

WebWork做的项目,在服务器启动时完成WebWork的框架初始化。具体是通过Web.xml中配置好的com.opensymphony.xwork.dispatcher.ServletDispatcher(FilterDispatcher)过滤器中的init(ServletConfig servletConfig)方法完成。

并且web.xml中配置好ServletDispatcher的映射,当用户用映射好的结尾资源请求浏览器时,ServletDispatcher会进行请求处理(ServletDispatcher是一个HttpServlet)。

具体实现是通过以下步骤:

1、通过ServletDispatcher中的init方法进行框架的初始化工作:

  1. public void init(ServletConfig servletConfig)  
  2.       throws ServletException  
  3.   {  
  4.       super.init(servletConfig);  
  5.       DispatcherUtils.initialize(getServletContext());  
  6.  
  7.    } 

2、init方法又同时调用DispatcherUtils类的initialize方法创建DispatcherUtils实例,同时间接调用DispatcherUtils类的init方法初始化Configuration配置,创建对象创建的工厂ObjectFactory和ObjectTypeDeterminer。

至此完成WebWork框架的初始化。

二、WebWork的用户请求处理过程

所有以web.xml中映射ServletDispatcher结尾的服务请求将由ServletDispatcher进行处理。

1、从用户请求的服务名中解析出对应Action的名称。

  1. public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {  
  2.   //....  
  3.     try  
  4.     {  
  5.         request = du.wrapRequest(request, getServletContext());  
  6.     }  
  7.     catch(IOException e)  
  8.     {  
  9.         String message = "Could not wrap servlet request with MultipartRequestWrapper!";  
  10.         LOG.error(message, e);  
  11.         throw new ServletException(message, e);  
  12.     }  
  13.     du.serviceAction(request, response, getServletContext(), mapping);  

2、遍历HttpServletRequest、HttpSession、ServletContext 中的数据,并将其复制到Webwork的Map中,为下一步创建Action实例打下基础。

实现:通过过调用DispatcherUtils的serviceAction方法中的Map extraContext = createContextMap(request, response, mapping, context);完成以上信息的封装。

3、以上一步封装好的信息为参数,调用ActionProxyFactory创建对应的ActionProxy实例。ActionProxyFactory 将根据Xwork 配置文件(xwork.xml)中的设定,创建ActionProxy实例,ActionProxy中包含了Action的配置信息(包括Action名称,对应实现类等等)。

实现:通过ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);//创建动态代理DefaultActionProxyFactory实现ActionProxyFactory的createActionProxy方法,返回new DefaultActionProxy(namespace, actionName, extraContext, true, true);DefaultActionProxy是对ActionProxy的默认实现,通过DefaultActionProxy类的DefaultActionProxy(namespace, actionName, extraContext, true, true)构造方法实例化DefaultActionProxy,同时得到用户请求的actionName及namespace,并通过config = ConfigurationManager.getConfiguration().getRuntimeConfiguration().getActionConfig(namespace, actionName);
ConfigurationManager的

  1. public static synchronized Configuration getConfiguration()  
  2. {  
  3.     if(configurationInstance == null)  
  4.     {  
  5.         configurationInstance = new DefaultConfiguration();  
  6.         try  
  7.         {  
  8.             configurationInstance.reload();  
  9.         }  
  10.         catch(ConfigurationException e)  
  11.         {  
  12.             configurationInstance = null;  
  13.             throw e;  
  14.         }  
  15.     } else  
  16.     {  
  17.         conditionalReload();  
  18.     }  
  19.     return configurationInstance;  

完成对xwork.xml(具体操作类是XmlConfigurationProvider)配置信息的读取。获得与此次请求相关的ActionConfig。

4、ActionProxy创建对应的Action实例,并根据配置进行一系列的处理程序。

通过DefaultActionProxy类的invocation = ActionProxyFactory.getFactory().createActionInvocation(this, extraContext);  

//通过createActionInvocation方法创建动作调用类ActionInvocation,处理被Action调用的方法

  1. privatevoid resolveMethod() {  
  2.         // if the method is set to null, use the one from the configuration  
  3.         // if the one from the configuration is also null, use "execute"  
  4.         if (!TextUtils.stringSet(this.method)) {  
  5.             this.method = config.getMethodName();  
  6.             if (!TextUtils.stringSet(this.method)) {  
  7.                 this.method = "execute";  
  8.             }  
  9.         }  

然后调用DispatcherUtils的serviceAction方法中的

  1. if (mapping.getResult() != null) {  
  2.                 Result result = mapping.getResult();  
  3.                 result.execute(proxy.getInvocation());  
  4.             } else {  
  5.                 proxy.execute();  

完成用户的最终要执行的action方法。

  1. public String execute() throws Exception {  
  2.         ActionContext nestedContext = ActionContext.getContext();  
  3.         ActionContext.setContext(invocation.getInvocationContext());  
  4.    
  5.         String retCode = null;  
  6.    
  7.         try {  
  8.             retCode = invocation.invoke();  
  9.         } finally {  
  10.             if (cleanupContext) {  
  11.                 ActionContext.setContext(nestedContext);  
  12.             }  
  13.         }  
  14.    
  15.         return retCode;  
  16.     } 

最终处理ActionContext对象,将Action调用提交给ActionInvocation处理。

5、 一旦Action方法返回,ActionInvocation就要查找xwork.xml文件中这个Action的结果码(Action Result Code)(一个String如success、input)所对应的result,然后执行这个result。通常情况下,result会调用JSP或FreeMarker模板来呈现页面。当呈现页面时,模板可以使用WebWork提供的一些标签,其中一些组件可以和ActionMapper一起工作来为后面的请求呈现恰当的URL。

下面我们来看action部分的定义:

  1. <action name="loginAction" class="loginAction"> 
  2.   <result name="success" type="dispatcher">/common/loginedHomeAction!init.action</result> 
  3.  </action> 

这里的result结点有一个type属性,这表示此action的结果应该怎样处理。

再来看看dispatcher类型的result是怎么定义的:

  1. <result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/> 

到这里就可以知道了处理是交给ServletDispatcherResult类来做的。

ServletDispatcherResult类继承了WebWorkResultSupport类,而WebWorkResultSupport实现了com.opensymphony.xwork.Result接口,此接口用来处理action的结果。WebWorkResultSupport类定义了一个抽象的方法——doExecute,此方法用于实现对Result的处理。

下面来看看ServletDispatcherResult是怎么处理的:

  1. public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {  
  2.  
  3.         PageContext pageContext = ServletActionContext.getPageContext();  
  4.  
  5.         if (pageContext != null) {  
  6.             pageContext.include(finalLocation);  
  7.         } else {  
  8.             HttpServletRequest request = ServletActionContext.getRequest();  
  9.             HttpServletResponse response = ServletActionContext.getResponse();  
  10.             RequestDispatcher dispatcher = request.getRequestDispatcher(finalLocation);  
  11.  
  12.             // if the view doesn't exist, let's do a 404  
  13.             if (dispatcher == null) {  
  14.                 response.sendError(404, "result '" + finalLocation + "' not found");  
  15.  
  16.                 return;  
  17.             }  
  18.  
  19.             // If we're included, then include the view  
  20.             // Otherwise do forward   
  21.             // This allow the page to, for example, set content type   
  22.             if (!response.isCommitted() && (request.getAttribute("javax.servlet.include.servlet_path") == null)) {  
  23.                 request.setAttribute("webwork.view_uri", finalLocation);  
  24.                 request.setAttribute("webwork.request_uri", request.getRequestURI());  
  25.  
  26.                 dispatcher.forward(request, response);  
  27.             } else {  
  28.                 dispatcher.include(request, response);  
  29.             }  
  30.         }  
  31.     } 

我们看到,最终调用的是dispatcher.forward(request, response);这样就可以成功转到我们的目标页了。

以下代码为DispatcherUtils中的serviceAction方法中的:

  1. public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping)  
  2.     throws ServletException  
  3. {  
  4.     Map extraContext = createContextMap(request, response, mapping, context);  
  5.     OgnlValueStack stack = (OgnlValueStack)request.getAttribute("webwork.valueStack");  
  6.     if(stack != null)  
  7.         extraContext.put("com.opensymphony.xwork.util.OgnlValueStack.ValueStack", new OgnlValueStack(stack));  
  8.     try  
  9.     {  
  10.         String namespace = mapping.getNamespace();  
  11.         String name = mapping.getName();  
  12.         String method = mapping.getMethod();  
  13.         String id = request.getParameter("__continue");  
  14.         if(id != null)  
  15.         {  
  16.             Map params = (Map)extraContext.get("com.opensymphony.xwork.ActionContext.parameters");  
  17.             params.remove("__continue");  
  18.             extraContext.put("__continue", id);  
  19.         }  
  20.         ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);  
  21.         proxy.setMethod(method);  
  22.         request.setAttribute("webwork.valueStack", proxy.getInvocation().getStack());  
  23.         if(mapping.getResult() != null)  
  24.         {  
  25.             Result result = mapping.getResult();  
  26.             result.execute(proxy.getInvocation());  
  27.         } else  
  28.         {  
  29.             proxy.execute();  
  30.         }  
  31.         if(stack != null)  
  32.             request.setAttribute("webwork.valueStack", stack);  
  33.     }  
  34.     catch(ConfigurationException e)  
  35.     {  
  36.         LOG.error("Could not find action", e);  
  37.         sendError(request, response, 404, e);  
  38.     }  
  39.     catch(Exception e)  
  40.     {  
  41.         String msg = "Could not execute action";  
  42.         LOG.error(msg, e);  
  43.         throw new ServletException(msg, e);  
  44.     }  

三、WebWork的执行流程图

WebWork的执行流程图

【编辑推荐】

  1. WebWork如何实现文件上传配置过程
  2. WebWork下载的实现
  3. 通过WebWork实现HelloWorld
  4. Tapestry 5组件事件详解
  5. Tapestry5的性能改进浅析

 

责任编辑:雪峰 来源: CSDN博客
相关推荐

2009-07-08 09:55:51

WebWork下载

2009-07-14 01:00:43

WebWorkActionConte

2009-07-16 14:08:14

webwork配置

2009-07-14 15:52:00

WebWork文件下载

2009-07-14 16:08:41

WebWork学习

2009-07-14 17:34:53

Webwork配置

2009-07-08 10:56:04

WebWork

2009-07-10 11:02:17

WebWork参数配置

2009-07-16 16:01:54

WebWork敏捷开发

2009-07-08 10:11:30

WebWork

2009-07-14 14:04:36

WebWork框架

2009-07-16 16:51:56

WebWork验证机制

2009-07-16 16:08:30

WebWork Act

2009-07-14 17:10:44

struts2webwork

2009-07-09 18:24:00

WebWork与Spr

2009-07-16 16:27:26

Struts WebW

2009-07-14 14:41:33

Webwork与Spr

2009-07-14 17:20:31

Webwork文件上传

2009-07-16 14:58:03

WebWork安装WebWork配置

2009-07-09 16:22:12

WebWork配置
点赞
收藏

51CTO技术栈公众号