现在的位置: 首页 > 综合 > 正文

关于Struts2中的ValueStack,ActionContext,OgnlContext的理解之二:Struts2中的OGNL

2014年02月13日 ⁄ 综合 ⁄ 共 5575字 ⁄ 字号 评论关闭

在我们理解了OGNL框架之下的表达式计算特性之后,我们简单做一总结如下:

        在OGNL中,表达式计算所依据的两部分为:root和OgnlConext离开这两个要素,ognl表达式就失去了意义,应该注意的是:这里的根root是一个单独的对象.我们访问根不需要加#,OgnlContext中的刚要加

1.    OGNL基本描述(在Xwork中):

         The biggestaddition that XWork provides on top of OGNL is the support for the ValueStack.While OGNL operates under the assumption there is only one "root",XWork's ValueStack concept(['kɔnsept]概念)requiresthere be many "roots".

XWork对OGNL表达式的”根”进行了较大扩充,用以支持ValueStack;正常当使用OGNL表达式时,都是在假定只有一个”根”的情况下使用的.XWork的ValueStack概念允许有多个”根”.

        For example, suppose we areusing standard OGNL (not using XWork) and there are two objects in theOgnlContext map: "foo" -> foo and "bar" -> bar andthat the foo object is also configured to be the single root object. Thefollowing code
illustrates(['iləstreit] 图示(说明)) how OGNL deals([di:l]处理) with these three situations([,sitju'eiʃən]情况,处境):

       例如:如果我们使用标准的OGNL表达式(不是XWork中的表达式)并且有两个对象foo,bar,而且已经把foo对象设置为根对象,则下图代码说明了ognl表达式怎么去处理以下三种情况:

#foo.blah // returns foo.getBlah()

#bar.blah // returns bar.getBlah()

blah      // returns foo.getBlah() because foo is the root

  在OGNL中,如果表达式没有使用#号,那么OGNL会从根对象中寻

      What this means is that OGNLallows many objects in the context, but unless the object you are trying to accessis the root, it must be prepended with a namespaces such as @bar. Now let'stalk about how XWork is a little different...

    意思就是说:OGNL允许”context”里有多个对象,除过我们试图去访问的根对象之外 ,其他对象必须以预先设定的命令格式比如像@bar的形式去访问.再来看看XWork框架有什么不同于一般的OGNL.

Useful Information

      In XWork, the entireValueStack is the root object in the context. Rather than having yourexpressions get the object you want from the stack and then get properties fromthat (ie: peek().blah), XWork has a special OGNL PropertyAccessor that
willautomatically look at the all entries in the stack (from the top down) until itfinds an object with the property you are looking for.

       在XWork框架里,整个ValueStack在context里算作一个根对象,相当于使用自有的表达式从”栈”里拿到你想拿到的那个对象 然后从这个对象里得到它的属性(比如:peek().blah),XWork框架有一个特有的OGNL属性计数器,这个计数器会自动的监视整个(进入到)栈(中的对象)(从栈顶到栈底),直到计数器查找到我们所要想的对象及属性.

       For example, suppose the stackcontains two objects: Animal and Person. Both objects have a "name"property, Animal has a "species" property, and Person has a"salary" property. Animal is on the top of the stack, and Person isbelow it. The
follow code fragments help you get an idea of what is going

on here:

       例如:假设栈里包含了两个对象”Animal”与”Person”,两者都有相同的属性”name”,Animal对象有一个species属性,Personal对象有一个salary属性,Animal位于栈顶,Person次之,下列代码片段帮忙我们得出一种”存取”它们的方法.

species    // call to animal.getSpecies()

salary     // call to person.getSalary()

name       // call to animal.getName() because animal is on the top

 

         In the last example, there wasa tie and so the animal's name was returned. Usually this is the desiredeffect, but sometimes you want the property of a lower-level object. To dothis, XWork has added support for indexes on the ValueStack.
All you have to dois:

         在最后的一行中,有一个两个对象的共同属性name,却返回的是Animal对象的name属性,通常,这个是我们所希望的结果,但有些时候我们想得到栈顶下面的对象,基于这一点,XWork框架在ValueStack中添加了对索引的支持:

[0].name   // call to animal.getName()

[1].name   // call to person.getName()

With expression like [0] ...[3] etc. Struts 2 will cut the stack and still(默认) return back a CompoundRoot(混合)object. To get the top of that particular(特定) stack cut, use 0.top

ognl expression

description

[0].top

would get the top of the stack cut starting from element 0 in the stack (similar to top in this case)

[1].top

would get the top of the stack cut starting from element 1 in the stack

2. 在struts2,关于Ognl的描述:

      OGNL is the Object Graph Navigation Language (see
http://www.ognl.org/
 for the full documentation of OGNL). Here, wewill cover a few examples of OGNL features that co-exist with the framework. Toreview basic concepts, refer to
OGNL Basics.

      The framework uses a standard naming context to
evaluateOGNL expressions. The top level object dealing with OGNL is a Map (usually referredas a context map or context). OGNL has a notion of there being a root (ordefault) object within the context. In expression,
the properties of the rootobject can be referenced without any special "marker" notion.References to other objects are marked with a pound sign (#).

       框架使用了一个统一的命名上下文来计算OGNL表达式,OGNL的顶层对象被划分为一个Map(通常被称为上下文Map或上下文),OGNL框架下的上下文环境中有一个基于根对象(或者称为之默认对象)的概念.体现在表达式中为:这个根对象(或者默认对象)的属性可以不用使用任何特殊的标记去表示,但如果要引用OGNL框架之下的其他非根对象,就必须使用#号来标识.

       The framework sets the OGNL context to be ourActionContext, and the value stack to be the OGNL root object. (The value stackis 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 theActionContext, including Maps representing the application, session, andrequest contexts. These objects coexist in the ActionContext, alongside thevalue stack (our OGNL root).

        struts2框架设置OGNL上下文为我们的ActionContext,并且ValueStack为OGNL上下文的根对象.(注意ValueStack是一组多个对象,但在OGNL中体现为一个对象),随ValueStack一起,struts2框架还放置了其它对象在ActionContext中,包含了诸如Application,session,request,attr的一些映射,这些对象围绕着ValueStack并一起存在于ActionContext中(我们称之为根)

       

        There are other objects in the context map. The diagram is for example only.
        还有一些其它对象在context map中,上图仅仅是一个示例.
        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.

        Action的一个实例总是被压入valuseStack,因为Action是在栈上,而这个栈就是OGNL根,引用Action中的属性可以省略#字符号,但如果去访问在ActionContext中的其它对象必须使用#号以它来告诉ognl不在根对象中查找,而是在ActionContext中其它对象中查找.

Referencing an Action property

<s:property value="postalCode"/>

Other (non-root) objects in the ActionContext can be rendered use the # notation.

<s:property value="#session.mySessionPropKey"/> or

<s:property value="#session['mySessionPropKey']"/> or

<s:property value="#request['mySessionPropKey']"/>

    The ActionContext is also exposed to Action classes via a static method.

      ActionContext同时通过一个静态方法来暴露给Action类,(换句话说可以在Action中通过.getCotext()方法来获得ActionContext)

      ActionContext.getContext().getSession().put("mySessionPropKey", mySessionObject);

抱歉!评论已关闭.