问题提出:

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()方法的源代码:

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) res;try {prepare.setEncodingAndLocale(request, response);prepare.createActionContext(request, response);prepare.assignDispatcherToThread();if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {chain.doFilter(request, response);} else {request = prepare.wrapRequest(request);ActionMapping mapping = prepare.findActionMapping(request, response, true);if (mapping == null) {boolean handled = execute.executeStaticResourceRequest(request, response);if (!handled) {chain.doFilter(request, response);}} else {execute.executeAction(request, response, mapping);}}} finally {prepare.cleanupRequest(request);}}

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

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

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

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

public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,ActionMapping mapping) throws ServletException {Map<String, Object> extraContext = createContextMap(request, response, mapping, context);// If there was a previous value stack, then create a new copy and pass it in to be used by the new ActionValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);boolean nullStack = stack == null;if (nullStack) {ActionContext ctx = ActionContext.getContext();if (ctx != null) {stack = ctx.getValueStack();}}if (stack != null) {extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));}String timerKey = "Handling request from Dispatcher";try {UtilTimerStack.push(timerKey);String namespace = mapping.getNamespace();String name = mapping.getName();String method = mapping.getMethod();Configuration config = configurationManager.getConfiguration();ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(namespace, name, method, extraContext, true, false);request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());// if the ActionMapping says to go straight to a result, do it!if (mapping.getResult() != null) {Result result = mapping.getResult();result.execute(proxy.getInvocation());} else {proxy.execute();}// If there was a previous value stack then set it back onto the requestif (!nullStack) {request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);}} catch (ConfigurationException e) {// WW-2874 Only log error if in devModeif (devMode) {String reqStr = request.getRequestURI();if (request.getQueryString() != null) {reqStr = reqStr + "?" + request.getQueryString();}LOG.error("Could not find action or result\n" + reqStr, e);} else {if (LOG.isWarnEnabled()) {LOG.warn("Could not find action or result", e);}}sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);} catch (Exception e) {if (handleException || devMode) {sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);} else {throw new ServletException(e);}} finally {UtilTimerStack.pop(timerKey);}}

继续往下进行,如下图:

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

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

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

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

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

public void <span style="background-color: rgb(255, 0, 0);">init</span>(ActionProxy proxy) {this.proxy = proxy;Map<String, Object> contextMap = createContextMap();// Setting this so that other classes, like object factories, can use the ActionProxy and other// contextual information to operateActionContext actionContext = ActionContext.getContext();if (actionContext != null) {actionContext.setActionInvocation(this);}<span style="background-color: rgb(51, 255, 51);">createAction(contextMap);</span>if (pushAction) {<span style="background-color: rgb(51, 255, 51);">stack.push(action);</span><span style="background-color: rgb(51, 255, 51);">contextMap.put("action", action);</span>}invocationContext = new ActionContext(contextMap);invocationContext.setName(proxy.getActionName());// get a new List so we don't get problems with the iterator if someone changes the listList<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());interceptors = interceptorList.iterator();}
protected void <span style="background-color: rgb(255, 0, 0);">createAction</span>(Map<String, Object> contextMap) {// load actionString timerKey = "actionCreate: " + proxy.getActionName();try {UtilTimerStack.push(timerKey);<span style="background-color: rgb(51, 255, 51);">action = objectFactory.buildAction(proxy.getActionName(), proxy.getNamespace(), proxy.getConfig(), contextMap);</span>} catch (InstantiationException e) {throw new XWorkException("Unable to intantiate Action!", e, proxy.getConfig());} catch (IllegalAccessException e) {throw new XWorkException("Illegal access to constructor, is it public?", e, proxy.getConfig());} catch (Exception e) {String gripe;if (proxy == null) {gripe = "Whoa!  No ActionProxy instance found in current ActionInvocation.  This is bad ... very bad";} else if (proxy.getConfig() == null) {gripe = "Sheesh.  Where'd that ActionProxy get to?  I can't find it in the current ActionInvocation!?";} else if (proxy.getConfig().getClassName() == null) {gripe = "No Action defined for '" + proxy.getActionName() + "' in namespace '" + proxy.getNamespace() + "'";} else {gripe = "Unable to instantiate Action, " + proxy.getConfig().getClassName() + ",  defined for '" + proxy.getActionName() + "' in namespace '" + proxy.getNamespace() + "'";}gripe += (((" -- " + e.getMessage()) != null) ? e.getMessage() : " [no message in exception]");throw new XWorkException(gripe, e, proxy.getConfig());} finally {UtilTimerStack.pop(timerKey);}if (actionEventListener != null) {<span style="background-color: rgb(51, 255, 51);">action = actionEventListener.prepare(action, stack);</span>}}

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

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

收获:

1、分析问题的方法;

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

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

转载于:https://www.cnblogs.com/KnightKitt/p/3918079.html

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

  1. Struts2中的值栈

    一 什么是值栈 值栈: struts2中提供的一种类似于域对象的工具, 用于struts2中的存值和取值. 每次访问Action的时候, 都会创建一个action对象, 而每个action对象中都存在 ...

  2. struts2中的值栈对象ValueStack

    ValueStack, 即值栈对象. 值栈对象: 是整个struts数据存储的核心,或者叫中转站. 用户每次访问struts的action,都会创建一个Action对象.值栈对象.ActionCont ...

  3. java 值栈的结构_Struts2 | 深入浅出理解struts2中的值栈

    在没有struts框架的时候,我们通常在Servlet中使用域对象进行存值和取值,将其作为载体来承载页面和后台之间的数据传递. 在struts2中,我们又有了一种新的机制来进行数据的传递. 那就是st ...

  4. Struts2的ValueStack(值栈)介绍

    1.在之前写的一些文章中,有用到<s:debug></s:debug>这个Struts2自带的标签,会在jsp页面中产生一个Debug按钮,点击这个按钮时,就会出现ValueS ...

  5. java struts2值栈ognl_Struts2 (三) — OGNL与值栈

    一.OGNL表达式 1.概述 1.1什么是OGNL ​ OGNL是Object-Graph Navigation Language的缩写,俗称对象图导航语言. 它是一种功能强大的表达式语言,通过它简单 ...

  6. 关于Struts2中的值栈与OGNL表达式

    1.1.1    OGNL概述: Object Graphic Navigation Language(对象图导航语言)的缩写 * EL     :OGNL比EL功能强大很多倍. 它是一个开源项目. ...

  7. struts2值栈分析

    前段日子对ognl表达式不是很理解,看了几本书上关于ognl表达式的描述后还是感觉很难,前几天学习了struts2中值栈的内容,现在感觉ognl表达式其实很容易. struts2中利用值栈来存储数据, ...

  8. Struts2的值栈和对象栈

    目录(?)[-] ValueStack 如何得到值栈: 如何将对象存入值栈: 让值栈执行表达式来获得值: 在JSP中跳过栈顶元素直接访问第二层: 在JSP中访问值栈对象本身(而不是它们的属性) Act ...

  9. java struts2值栈ognl_Struts2的值栈和OGNL牛逼啊

    Struts2的值栈和OGNL牛逼啊 一 值栈简介: 值栈是对应每个请求对象的一套内存数据的封装,Struts2会给每个请求创建一个新的值栈,值栈能够线程安全的为每个请求提供公共的数据存取服务. 二 ...

  10. struts2 jsp跳转action 404_Struts2 学习笔记(三)

    1. Struts2的拦截器 1.1 拦截器概述 1.1.1 什么是拦截器 Interceptor:拦截器,起到拦截Action的作用: Filter:过滤器,过滤从客户端向服务器发送的请求: Int ...

最新文章

  1. Failed to instantiate one or more classes
  2. telnetd运行需要什么条件_申请日本研究生需要什么条件
  3. href=#与href=javascript:void(0)的区别
  4. 『HTML5制造仿JQuery作用』减速
  5. Cisco路由器故障诊断技术专题
  6. 初学java---第二课《接收控制台(console)输入的方法》
  7. python项目部署到url_项目上线部署
  8. python os库使用
  9. 一文看尽 TensorFlow“奋斗史”!| CSDN 博文精选
  10. 20个基本电路图讲解_12v太阳能充电电路图大全
  11. 常见英文缩写 (持续更新……)
  12. 国外网站整理出十大必须拥有的虚拟化工具
  13. 计算机拼音排序怎么操作,Excel如何按笔画和拼音进行排序,并且以下数字也会随之改变...
  14. oracle11g怎么打开oem,oracle11g启动OEM
  15. 计算机绘出一条虚线算法步骤,计算机图形学 第二章 基本图形生成算法.ppt
  16. 计算机硬盘使用率测试软件,测试硬盘速度的10款软件
  17. Cortex-A7中断系统
  18. 操作系统中怎么预防死锁呢?简单列举几种方法!
  19. 不需要登录微信,发送消息python
  20. 计算机毕业设计Java金融业撮合交易系统(源码+系统+mysql数据库+lw文档)

热门文章

  1. 获取表结构信息(字段名,类型,长度,精度,小数位数,主键,自动增长)...
  2. 让Xcode日志输出中文
  3. mybatis 3.2.7 与 spring mvc 3.x、logback整合
  4. 学习jQuery.Deferred
  5. label字符自动换行(转自网络)
  6. (原創) 如何控制DE2 VGA輸出時某座標的顏色? (IC Design) (DE2) (Quartus II)
  7. TCP/IP协议与Http协议的区别
  8. Mybatis 常用开发注解(批量插入)
  9. 编写可维护的javascript 笔记2(注释)
  10. NEsper Nuget包