通过debug过程分析Struts2什么时候将Action对象放入了值栈ValueStack中

来源:互联网 发布:ip自动更换软件 编辑:程序博客网 时间:2024/05/22 23:13

问题提出:

1、Struts2框架在什么时候将Action对象放到了值栈ValueStack的栈顶了?

2、在哪里设置Debug断点能够最恰当的观察到这一过程?

问题解决:

2、我们知道,在值栈ValueStack中有两个逻辑部分Map栈和对象栈ObjectStack,而Action对象是被默认放在了对象栈的栈顶的(这一点我们通过<s:debug/>标签可以在页面中看到),因此我们将该断点设置在对象栈所对应的类CompoundRoot的push方法中最合适。

1、通过Debug过程来分析值栈ValueStack的变化过程:

1)当我们在CompoundRoot类的push方法中打上断点后,程序执行到此处,如下图:


 2)这时,我们从Struts2框架的入口StrutsPrepareAndExecuteFilter类开始分析。在Debug视图中,我们将程序定位到StrutsPrepareAndExecuteFilter类,如下图:


附上StrutsPrepareAndExecuteFilter中doFilter()方法的源代码:

    

[java] view plain copy
  1. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {  
  2.   
  3.         HttpServletRequest request = (HttpServletRequest) req;  
  4.         HttpServletResponse response = (HttpServletResponse) res;  
  5.   
  6.         try {  
  7.             prepare.setEncodingAndLocale(request, response);  
  8.             prepare.createActionContext(request, response);  
  9.             prepare.assignDispatcherToThread();  
  10.             if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {  
  11.                 chain.doFilter(request, response);  
  12.             } else {  
  13.                 request = prepare.wrapRequest(request);  
  14.                 ActionMapping mapping = prepare.findActionMapping(request, response, true);  
  15.                 if (mapping == null) {  
  16.                     boolean handled = execute.executeStaticResourceRequest(request, response);  
  17.                     if (!handled) {  
  18.                         chain.doFilter(request, response);  
  19.                     }  
  20.                 } else {  
  21.                     execute.executeAction(request, response, mapping);  
  22.                 }  
  23.             }  
  24.         } finally {  
  25.             prepare.cleanupRequest(request);  
  26.         }  
  27.     }  

这里我们可以看到,doFilter()方法中通过调用execute对象的executeAction()方法来执行Action,接下来我们继续看一看executeAction()方法,如下图:


可以看出executeAction()方法中只是调用了dispatcher对象的serviceAction()方法,因此,我们再继续查看serviceAction()方法,如下图:


可以看到,在serviceAction()方法中实际上是创建了一个Action的代理类ActionProxy的对象,而并没有直接去执行Action。这是因为在执行Action之前Struts2还要去调用许多的拦截器,因此创建了Action的代理类。

附Dispatcher类中serviceAction()方法的源代码:

[java] view plain copy
  1. public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,  
  2.                               ActionMapping mapping) throws ServletException {  
  3.   
  4.         Map<String, Object> extraContext = createContextMap(request, response, mapping, context);  
  5.   
  6.         // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action  
  7.         ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);  
  8.         boolean nullStack = stack == null;  
  9.         if (nullStack) {  
  10.             ActionContext ctx = ActionContext.getContext();  
  11.             if (ctx != null) {  
  12.                 stack = ctx.getValueStack();  
  13.             }  
  14.         }  
  15.         if (stack != null) {  
  16.             extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));  
  17.         }  
  18.   
  19.         String timerKey = "Handling request from Dispatcher";  
  20.         try {  
  21.             UtilTimerStack.push(timerKey);  
  22.             String namespace = mapping.getNamespace();  
  23.             String name = mapping.getName();  
  24.             String method = mapping.getMethod();  
  25.   
  26.             Configuration config = configurationManager.getConfiguration();  
  27.             ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(  
  28.                     namespace, name, method, extraContext, truefalse);  
  29.   
  30.             request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());  
  31.   
  32.             // if the ActionMapping says to go straight to a result, do it!  
  33.             if (mapping.getResult() != null) {  
  34.                 Result result = mapping.getResult();  
  35.                 result.execute(proxy.getInvocation());  
  36.             } else {  
  37.                 proxy.execute();  
  38.             }  
  39.   
  40.             // If there was a previous value stack then set it back onto the request  
  41.             if (!nullStack) {  
  42.                 request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);  
  43.             }  
  44.         } catch (ConfigurationException e) {  
  45.             // WW-2874 Only log error if in devMode  
  46.             if (devMode) {  
  47.                 String reqStr = request.getRequestURI();  
  48.                 if (request.getQueryString() != null) {  
  49.                     reqStr = reqStr + "?" + request.getQueryString();  
  50.                 }  
  51.                 LOG.error("Could not find action or result\n" + reqStr, e);  
  52.             } else {  
  53.                 if (LOG.isWarnEnabled()) {  
  54.                     LOG.warn("Could not find action or result", e);  
  55.                 }  
  56.             }  
  57.             sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);  
  58.         } catch (Exception e) {  
  59.             if (handleException || devMode) {  
  60.                 sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);  
  61.             } else {  
  62.                 throw new ServletException(e);  
  63.             }  
  64.         } finally {  
  65.             UtilTimerStack.pop(timerKey);  
  66.         }  
  67.     }  
继续往下进行,如下图:






我们可以看到在上面的执行过程中,主要工作都是在创建Action的代理类对象proxy,在创建完成之后,该代理类对象proxy调用prepare()方法做了一些初始化的工作。

继续往下查看prepare()方法:


在prepare()方法(这是StrutsActionProxy的父类DefaultActionProxy的prepare()方法)中可以看到,其执行了invocation对象的init()方法,在该方法中this就是StrutsActionProxy类的对象,也即ActionProxy类的对象,就是Action的代理类。继续来看init()方法的具体实现:


到这里我们已经能够比较清楚的看到了Struts2框架将Action对象放到值栈中。注意,这里的Action对象中的相关属性(如提交的表单中的参数)并没有被赋上值,直到真正的执行Action对象中相应的方法时,其相关属性才被赋值。

附DefaultActionInvocation类中init()方法与createAction()方法的源代码:

[java] view plain copy
  1. public void <span style="background-color: rgb(255, 0, 0);">init</span>(ActionProxy proxy) {  
  2.         this.proxy = proxy;  
  3.         Map<String, Object> contextMap = createContextMap();  
  4.   
  5.         // Setting this so that other classes, like object factories, can use the ActionProxy and other  
  6.         // contextual information to operate  
  7.         ActionContext actionContext = ActionContext.getContext();  
  8.   
  9.         if (actionContext != null) {  
  10.             actionContext.setActionInvocation(this);  
  11.         }  
  12.   
  13.         <span style="background-color: rgb(51, 255, 51);">createAction(contextMap);</span>  
  14.   
  15.         if (pushAction) {  
  16.             <span style="background-color: rgb(51, 255, 51);">stack.push(action);</span>  
  17.             <span style="background-color: rgb(51, 255, 51);">contextMap.put("action", action);</span>  
  18.         }  
  19.   
  20.         invocationContext = new ActionContext(contextMap);  
  21.         invocationContext.setName(proxy.getActionName());  
  22.   
  23.         // get a new List so we don't get problems with the iterator if someone changes the list  
  24.         List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());  
  25.         interceptors = interceptorList.iterator();  
  26.     }  

[java] view plain copy
  1. protected void <span style="background-color: rgb(255, 0, 0);">createAction</span>(Map<String, Object> contextMap) {  
  2.         // load action  
  3.         String timerKey = "actionCreate: " + proxy.getActionName();  
  4.         try {  
  5.             UtilTimerStack.push(timerKey);  
  6.             <span style="background-color: rgb(51, 255, 51);">action = objectFactory.buildAction(proxy.getActionName(), proxy.getNamespace(), proxy.getConfig(), contextMap);</span>  
  7.         } catch (InstantiationException e) {  
  8.             throw new XWorkException("Unable to intantiate Action!", e, proxy.getConfig());  
  9.         } catch (IllegalAccessException e) {  
  10.             throw new XWorkException("Illegal access to constructor, is it public?", e, proxy.getConfig());  
  11.         } catch (Exception e) {  
  12.             String gripe;  
  13.   
  14.             if (proxy == null) {  
  15.                 gripe = "Whoa!  No ActionProxy instance found in current ActionInvocation.  This is bad ... very bad";  
  16.             } else if (proxy.getConfig() == null) {  
  17.                 gripe = "Sheesh.  Where'd that ActionProxy get to?  I can't find it in the current ActionInvocation!?";  
  18.             } else if (proxy.getConfig().getClassName() == null) {  
  19.                 gripe = "No Action defined for '" + proxy.getActionName() + "' in namespace '" + proxy.getNamespace() + "'";  
  20.             } else {  
  21.                 gripe = "Unable to instantiate Action, " + proxy.getConfig().getClassName() + ",  defined for '" + proxy.getActionName() + "' in namespace '" + proxy.getNamespace() + "'";  
  22.             }  
  23.   
  24.             gripe += (((" -- " + e.getMessage()) != null) ? e.getMessage() : " [no message in exception]");  
  25.             throw new XWorkException(gripe, e, proxy.getConfig());  
  26.         } finally {  
  27.             UtilTimerStack.pop(timerKey);  
  28.         }  
  29.   
  30.         if (actionEventListener != null) {  
  31.             <span style="background-color: rgb(51, 255, 51);">action = actionEventListener.prepare(action, stack);</span>  
  32.         }  
  33.     }  

查看值栈OgnlValueStack类的push()方法,如下图:

可以看到,它确实是调用了CompoundRoot对象的push()方法将Action对象放到了值栈中对象栈ObjectStack的栈顶。


收获:

1、分析问题的方法;

2、通过该过程清楚了Struts2值栈的变化过程;

2、进一步学习了Eclipse的debug使用方法。

阅读全文
0 0
原创粉丝点击