从调试角度理解ActionContext、OgnlContext、OgnlValueStack的关系

被调试代码:

 

   package web;

    import java.util.Map;

    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts2.ServletActionContext;
    import org.apache.struts2.StrutsConstants;
    import org.apache.struts2.StrutsStatics;

    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.util.ValueStack;

    public class LoginAction implements Action {

        private String userName;

       
        public void setUserName(String userName) {
            this.userName = userName;
        }

       
        public String getUserName() {
            return userName;
        }

        public String doLogin() {
            ActionContext cxt = ActionContext.getContext();
            ValueStack vstack = cxt.getValueStack();
           
            //返回前下断点
            return Action.SUCCESS;
        }

        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            return null;
        }
       
    }


访问url地址:http://localhost:8080/strutsdemo/doLogin.action?userName=jiang
被观察的表达式:
 ActionContext.getContext();
 ActionContext.getContext().getValueStack();
从调试角度理解ActionContext、OgnlContext、OgnlValueStack的关系观察结论:
  1、从ActionContext对象中可以取得OgnlValueStack对象
  2、ActionContext中持有的属性context和OgnlValueStack对象中持有的对象属性context的类型都是OgnlContext类型,其实是同一个对象
  3、Ognl持有一个root属性,这个属性存放的是Struts Ognl的根对象
   OGNL三要素:
  1.
expression 求值表达式——首先会被解析成对象树
  2.root object 根对象——默认的操作对象
  3.context OGNL执行环境——OGNL执行的上下文环境
OGNL context是一个Map结构,ognl.OgnlContext类implements Map接口,root对象也在context里面,并且做这一个特殊的对象处理,具体表现为对root  对象的操作不需要加#指示符号(并且加上了#一定取不到root对象里面的值)。
继续展开root属性节点:
从调试角度理解ActionContext、OgnlContext、OgnlValueStack的关系4、从root的对象名称我们都可以看出他表达的含义:CompoundRoot表示符合的根对象,根据原始的Ognl,根对象是单个对象。而 Struts2扩展了这个概念,这个跟对象不在仅仅只代表单个对象而可以是多个对象,这里我们从调试中可以看出它包含当前Action对象和 DefaultTextProvider对象。CommpoundRoot为何可以包含多个对象?
从调试角度理解ActionContext、OgnlContext、OgnlValueStack的关系


很简单,因为它集成自ArrayList集合,从下面扩展的方法中可以看出它实现了对集合栈数据结构方式的存取能力。我们看看另一段关于这个CompoundRoot的解释:
There can be many "root" objects. XWork中的表示根对象是CompoundRoot对象。CompoundRoot类extends ArrayList类。因为是一个List,里面可以放置多个对象,而这些对象经过XWork的改进对于OGNL表达式引擎来说都是root objects。XWork has a special OGNL PropertyAccessor that will automatically look at the all entries in the stack (in fact the CompoundRoot list) (from the top down) until it finds an object with the property you are looking for.


再来重点观察OgnlContext这个非常重要的容器:

从调试角度理解ActionContext、OgnlContext、OgnlValueStack的关系
5、OgnlContext实现了Map接口,这里_values私有属性存放的是Map的键值对信息,我们展开里面的table节点观察里面存放了什么秘密:从调试角度理解ActionContext、OgnlContext、OgnlValueStack的关系

从调试角度理解ActionContext、OgnlContext、OgnlValueStack的关系

从调试角度理解ActionContext、OgnlContext、OgnlValueStack的关系

6、我只是展开了部分,从中我们可以看出Struts2放置请求表单参数、请求对象本身、session对象、application 对象和application对象属性等等到OGNL栈(事实上放在OgnlContex).Struts 2 places request parameters and request, session, and application attributes on the OGNL stack (in fact the OGNL context).
  

小结:
 1、Struts2数据传输DataTransfer的核心对象是OgnlValueStack、 OgnlContext,OgnlValueStack持有OgnlContext和root对象,注意:OgnlContext和root对象是 ognl的核心要素【参考我的转载http://blog.csdn.net/jiangtongcn/article/details/7669871】。
 2、OgnlContext这个容器存放了所有本次Web请求响应的所有相关信息对象,root的存放的多个根对象。
 3、Struts2 Ognl表达式的解析主要依赖OgnlValueStack去完成。表达式中如果没有带#,如:“emp.name”,那么OgnlValueStack 将去根对象中去遍历每个对象看是否某个对象的方法是否匹配"emp.getName()",匹配则执行,否则跑出OgnlExcpetion异常。如果带有#,则去Ognl上下文中去寻找是否有满足emp.name属性的key,有责返回。
 4、Struts2的ActionContext类只是为了访问ValueStack而提供的一个Facade【门面设计模式】,为程序员访问各种信息提供一个一致的界面。
 5、使用Ognl表达式从OgnlValueStack中取出值:
 代码:

    public class LoginAction implements Action {

        private String userName;

       
        public void setUserName(String userName) {
            this.userName = userName;
        }

       
        public String getUserName() {
            return userName;
        }

        public String doLogin() {
            ActionContext cxt = ActionContext.getContext();
            ValueStack vstack = cxt.getValueStack();

            Employee emp1 = new Employee(){{
                setUserName("小强");
            }};
            Employee emp2 = new Employee(){{
                setUserName("秋香");
            }};
           
            //向栈栈顶压入两个员工对象
            vstack.set("emp1", emp1);
            vstack.setValue("emp2",emp2);
           
           
            //向request请求参数集合中放入一个属性和属性值
            cxt.getParameters().put("pwd","123123");
            //向request请求中放入一个属性和属性值
            ((Map<String,Object>)cxt.get("request")).put("pwd", "112233");
            //向Session中放入一个属性和属性值
            cxt.getSession().put("pwd", "112211");
            //向Application中放入一个属性和属性值
            cxt.getApplication().put("pwd", "112222");
           
            //向OgnlContext上下文放入一个属性和属性值
            cxt.put("pwd", "123321");
           
    //       
           
            //返回前下断点
            return Action.SUCCESS;
        }

        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            return null;
        }
       
    }

    class Employee {

        private String userName;

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }
    }


 

    观察Ognl表达式的值:
            System.out.println("vstack.findValue("userName") ==> "+vstack.findValue("userName"));   
            System.out.println("vstack.findValue("emp1.userName") ==> "+vstack.findValue("emp1.userName"));
            System.out.println("vstack.findValue("emp2.userName") ==> "+vstack.findValue("emp2.userName"));
            System.out.println("vstack.findValue("#parameters.userName") ==> "+vstack.findValue("#parameters.userName"));
            System.out.println("vstack.findValue("#request.pwd") ==> "+vstack.findValue("#request.pwd"));
            System.out.println("vstack.findValue("#session.pwd") ==> "+vstack.findValue("#session.pwd"));
            System.out.println("vstack.findValue("#application.pwd") ==> "+vstack.findValue("#application.pwd"));
            System.out.println("vstack.findValue("pwd") ==> "+vstack.findValue("pwd"));
            System.out.println("vstack.findValue("#attr.pwd") ==> "+vstack.findValue("#attr.pwd"));

            结果如下:
            vstack.findValue("userName") ==> jiang
    vstack.findValue("emp1.userName") ==> 小强
    vstack.findValue("emp2.userName") ==> 秋香
    vstack.findValue("#parameters.userName") ==> [Ljava.lang.String;@1e9b48b
    vstack.findValue("#request.pwd") ==> 112233
    vstack.findValue("#session.pwd") ==> 112211
    vstack.findValue("#application.pwd") ==> 112222
    vstack.findValue("pwd") ==> 123321
    vstack.findValue("#attr.pwd") ==> 112233


 
http://www.linuxso.com/architecture/34752.html
原文地址:https://www.cnblogs.com/kabi/p/5182754.html