zoukankan      html  css  js  c++  java
  • ActionContext、ValueStack、OGNL

     Struts2中的OGNL Context 就是ActionContext,如下图Struts2的OGNL的结构示意图
                                     |
                                     |--application
                                     |
                                     |--session
               context map---|
                                     |--value stack(root)
                                     |
                                     |--request
                                     |
                                     |--parameters
                                     |
                                     |--attr (searches page, request, session, then application scopes)
     
     
     
    The framework uses a standard naming context to evaluate OGNL expressions. The top level object dealing with OGNL is a Map (usually referred as a context map or context). OGNL has a notion of there being a root (or default) object within the context. In expression, the properties of the root object can be referenced without any special "marker" notion. References to other objects are marked with a pound sign (#).
    The framework sets the OGNL context to be our ActionContext, and the value stack to be the OGNL root object. (The value stack is a set of several objects, but to OGNL it appears to be a single object.) Along with the value stack, the framework places other objects in the ActionContext, including Maps representing the application, session, and request contexts. These objects coexist in the ActionContext, alongside the value stack (our OGNL root).
     
    The Action instance is always pushed onto the value stack. Because the Action is on the stack, and the stack is the OGNL root, references to Action properties can omit the # marker. But, to access other objects in the ActionContext, we must use the # notation so OGNL knows not to look in the root object, but for some other object in the ActionContext.


    1、ActionContext 

    当struts2框架接收到一个HTTP请求时,它立刻创建一个ActionContext、ValueStack、Action对象 

    ActionContext里有6个对象,分别是valueStack、parameters、request、session、application、attr 

    一个OGNL表达式,必须选择ActionContext中的一个对象作为根对象(root),默认情况下,是选择valueStack作为根对象,如果需要使用另外5个对象作为根对象,需要加上#前缀 

    例如: 

    Java代码  收藏代码
    1. <s:property value="#session.xxx" />  


    如果不加#前缀,则默认使用valueStack作为根对象,这也是最常见的情况,即#valueStack.xxx,相当于xxx 

    2、ValueStack(值栈) 

    ValueStack中可以存储很多对象,它的一个特性是,它是一个虚拟对象,它可以将自己持有的对象的属性,当成是自己的属性 

    比如说,ValueStack中有一个Action对象,而Action对象有一个name字段。那么当用OGNL表达式取name的值的时候,不需要${action.name},而是可以直接${name} 

    ValueStack是一个栈的数据结构(FILO),最后进入值栈的对象,总是在ValueStack的栈顶,这个结论很重要,因为栈顶的元素的值,会覆盖栈底的同名元素的值。 

    比如说,ValueStack的栈底是一个Action对象,持有一个name字段;栈顶是一个Model对象,也持有一个name字段,那么用${name},取出来的永远是Model对象的name字段,Action对象的name字段是不可见的 

    3、OGNL表达式 

    这个可以分为2种场景,一种是在<s:>标签的属性里(比如<s:property value="" />),一种是在jsp页面的其他地方 

    在<s:>标签的属性里时,要看这个属性定义的类型是什么,如果是string类型,那么属性的值会被当做普通的string,如果不是string类型,那么属性的值会被直接当成OGNL的表达式 

    比如说<s:property value="" />,这个标签的value属性的类型是object,那么这个value的值,就会被直接作为OGNL表达式进行解析 

    如果想在string类型的属性中使用OGNL表达式,就需要加上${}或者%{} 

    在jsp页面的其他地方时(即不在<s:>标签内部),任何情况下都会当成string来处理,这时候如果想使用OGNL表达式,也需要加上${}或者%{} 

    4、关于${}和%{}的区别 

    根据文档里的描述,OGNL表达式应该用%{}来表示。可是我在实际应用中,基本不会使用<s:>标签,并且都是用${}来取值的,也没有发现任何不妥,不知道是不是版本的原因

  • 相关阅读:
    JAVA基础知识-java文化基础和运行环境
    一个获得jdbc方式Connection的静态方法
    Hibernate学习笔记(二)
    JVM学习笔记
    chrome常用插件
    面向站长和网站管理员的Web缓存加速指南
    OFBiz进阶之环境搭建(eclipse)
    OFBiz之SVN下载地址
    OFBiz进阶之HelloWorld(一)创建热部署模块
    Sublime Text 3插件之SublimeTmpl:新建文件的模版插件
  • 原文地址:https://www.cnblogs.com/SpringSmallGrass/p/3005980.html
Copyright © 2011-2022 走看看