Struts2学习笔记-OGNL

从Struts2出来就开始关注,但一直没有全面的去学习和研究过,最近 计划把Struts2的官方

文档好好的看一遍,并会在这里将学习的内容和 遇见的问题进行发布,欢迎大家来指导和讨

论(由于本人英文水平有限,有很多翻译不正确的地方希望大家进行指正,第一次发表这种

学习笔记,也希望得到大家的鼓励

好了,废话少说,直接入题...

 

XWork 提供了通过 OGNLValueStack 操作的支持,标准 OGNL 要求只对一个 root 操作, XWorkValueStack 概念要求支持多个 roots 操作。

1)标准 OGNL 使用举例:

假设在 OgnlContext map 中有两个对象: ”foo”->foo and “bar”->bar 并且 foo 对象被配置为单一的 root 对象,下边的例子解释了 OGNL 处理的三种情形:

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

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

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

以上说明,OGNL 允许在context 中有多个对象,但是如果你要访问的对象不是root 的话,必须在要访问的对象前边加上一个类似@bar 的命名空间。

2) XWork 有一个专门的 OGNL PropertyAccessor, 该属性访问器将从上到下自动查找整个 stack ,直到找到你要找的属性对应的对象。

For example: 假设 stack 中包含有两个对象: Animal and Person ,这两个对象都有“ name ”属性, Animal 有一个“ species ”属性, Person 有一个“ salary ”属性, Animalstack 的顶部, PersonAnimal 的下边,下边的代码片断帮助我们理解其原理:

species    // call to animal.getSpecies()

salary     // call to person.getSalary()

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

上面的代码片断中返回的是 animalname, 通常这是我们想得到的效果,但是有时我们想得到下级对象的同样的属性,针对这种情况, XWorkValueStack 中添加了索引的支持,因此,可使用以下代码片断分别得到 AnimalPerson 对象的 name 属性:

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

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

With expression like [0] ... [3] etc. WebWork will cut the stack and still returned 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

3)访问静态属性

OGNL 支持静态属性和静态方法的访问,具体代码片断举例如下:

@some.package.ClassName@FOO_PROPERTY


@some.package.ClassName@someMethod()


当然, XWork 允许使用 ”vs”prefix 避免指定完整的包名访问 Action 类中的静态方法和静态属性,如:

@vs@FOO_PROPERTY

@vs@someMethod()

 

@vs1@FOO_PROPERTY

@vs1@someMethod()

 

@vs2@BAR_PROPERTY

@vs2@someOtherMethod()

vs ”代表“value stack.

The important thing to note here is that if the class name you specify is just "vs", the class for the object on the top of the stack is used. If you specify a number after the "vs" string, an object's class deeper in the stack is used instead. (大家可对照上面的举例来自行理解该段说明)

4) Struts2 指定的对象

Struts2request parameters and request, session, and application attributes 都存放在 OGNL stack 里面,可通过如下方式访问它们:

name

value

#parameters['foo'] or #parameters.foo

request parameter ['foo'] (request.getParameter())

#request['foo'] or #request.foo

request attribute ['foo'] (request.getAttribute())

#session['foo'] or #session.foo

session attribute 'foo'

#application['foo'] or #application.foo

ServletContext attributes 'foo'

#attr['foo'] or #attr.foo

Access to PageContext if available, otherwise searches request/session/application respectively

 

FAQ:

1)在 jsp 2.1 中, # 至今被用作 JSP EL ,在这种情况下就会引起和在应用中使用 OGNL# 操作产生冲突的问题,对于这种情况,可通过在 web.xml 配置文件中添加以下配置片断在 JSP 2.1 容器中禁用 JSP EL

<jsp-config>

    <jsp-property-group>

      <url-pattern>*.jsp</url-pattern>

      <el-ignored> true </el-ignored>

</jsp-property-group>

</jsp-config>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值