XWork-specific language features
The biggest addition 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 requires there be many "roots".
For example, suppose we are using standard OGNL (not using XWork) and there are two objects in the OgnlContext map: "foo" -> foo and "bar" -> bar and that the foo object is also configured to be the single root object. The following code illustrates how OGNL deals with these three situations:
#foo.blah // returns foo.getBlah()
#bar.blah // returns bar.getBlah()
blah // returns foo.getBlah() because foo is the root
What this means is that OGNL allows many objects in the context, but unless the object you are trying to access is the root, it must be prepended with a namespaces such as @bar. Now let's talk about how XWork is a little different...
Useful Information
In XWork, the entire ValueStack is the root object in the context. Rather than having your expressions get the object you want from the stack and then get properties from that (ie: peek().blah), XWork has a special OGNL PropertyAccessor that will automatically look at the all entries in the stack (from the top down) until it finds an object with the property you are looking for.
For example, suppose the stack contains 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 is below it. The follow code fragments help you get an idea of what is going on here:
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 was a tie and so the animal's name was returned. Usually this is the desired effect, but sometimes you want the property of a lower-level object. To do this, XWork has added support for indexes on the ValueStack. All you have to do is:
[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
Accessing static properties
OGNL supports accessing static properties as well as static methods. As the OGNL docs point out, you can explicitly call statics by doing the following:
@some.package.ClassName@FOO_PROPERTY
@some.package.ClassName@someMethod()
However, XWork allows you to avoid having to specify the full package name and call static properties and methods of your action classes using the "vs" prefix:
@vs@FOO_PROPERTY
@vs@someMethod()
@vs1@FOO_PROPERTY
@vs1@someMethod()
@vs2@BAR_PROPERTY
@vs2@someOtherMethod()
"vs" stands for "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.
Differences from the WebWork 1.x EL
Besides the examples and descriptions given above, there are a few major changes in the EL since WebWork 1.x. The biggest one is that properties are no longer accessed with a forward slash (/) but with a dot (.). Also, rather than using ".." to traverse down the stack, we now use "[n]" where n is some positive number. Lastly, in WebWork 1.x one could access special named objects (the request scope attributes to be exact) by using "@foo", but now special variables are accessed using "#foo". However, it is important to note that "#foo" does NOT access the request attributes. Because XWork is not built only for the web, there is no concept of "request attributes", and thus "#foo" is merely a request to another object in the OgnlContext other than the root.
Old Expression New Expression
foo/blah foo.blah
foo/someMethod() foo.someMethod()
../bar/blah [1].bar.blah
@baz not directly supported, but #baz is similar
. top or [0]
Struts 2 Named Objects
Struts 2 places request parameters and request, session, and application attributes on the OGNL stack. They may be accessed as shown below.
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
分享到:
相关推荐
WebWork教程-表达式与言EL和OGNL- babydavic(王汉祥)的专栏- CSDNBlog.zipWebWork教程-表达式与言EL和OGNL- babydavic(王汉祥)的专栏- CSDNBlog.zip
4. OGNL(Object-Graph Navigation Language):WebWork2使用OGNL作为默认表达式语言,用于在视图层和模型层之间传递数据。 二、WebWork2快速入门 1. 安装配置:首先,需要在项目中引入WebWork2的依赖库,配置web....
本文将详细介绍Struts2、Webwork以及Ognl的相关知识点,并提供入门指导。 **Struts2框架** Struts2是在原有的Struts1基础上发展起来的,它整合了Webwork框架的优势,提供了更灵活的控制流和更强大的拦截器机制。...
8. **插件和扩展**:WebWork2支持丰富的插件系统,如OGNL表达式语言、Tiles布局、AOP等,它们通过简单的配置即可引入到项目中。 9. **测试与调试**:WebWork2提供了良好的测试支持,你可以配置Action的模拟对象以...
1. OGNL表达式语言:WebWork2 使用OGNL(Object-Graph Navigation Language)作为默认的表示层语言,允许开发者在视图中直接访问模型对象的属性。 2. 数据验证:WebWork2 内置了数据验证框架,可以通过注解或XML...
10. **jar包**:压缩包中的jar文件包含了WebWork2运行所需的库,包括核心库、OGNL库、AOP库等,它们是WebWork2框架正常工作的基础。 通过学习和研究这个WebWork2实例源代码,你可以深入了解J2EE应用的开发模式,...
Webwork2 Guide 则会讲解 Webwork2 的核心概念,如动作映射、拦截器栈、OGNL 使用等。DWR 中文文档则会介绍如何设置 DWR,如何在客户端和服务器端交互,以及如何处理安全性问题。 通过学习这三个技术,开发者不仅...
WebWork2是一款基于Java的轻量级Web应用框架,它为开发者提供了强大的MVC(Model-View-Controller)架构支持,使得构建动态、数据驱动的Web应用变得更加简单和高效。这款框架在2000年代中期较为流行,是Struts的一个...
4. **Value Stack(值栈)**:值栈是WebWork2中的一个核心概念,它存储了Action实例和其他对象,这些对象可以通过OGNL(Object-Graph Navigation Language)表达式在视图层访问。 5. **Request/Session/...
3. **视图与模板**:探讨JSP、FreeMarker或其他模板引擎在WebWork2中的使用,解释如何创建和管理视图组件,以及如何通过OGNL(Object-Graph Navigation Language)表达式在视图中访问模型数据。 4. **模型**:讨论...
随着时间的发展,OGNL逐渐演变为处理复杂数据关系的语言,广泛应用于多个知名Web框架,如WebWork(Struts2的前身)和Tapestry。 OGNL的语法简洁而强大,其基本单位是“导航链”,它可以用来访问对象的属性、调用...
WebWork2是一个基于Java的轻量级Web应用框架,它为开发者提供了构建高效、可维护的Web应用程序的强大工具。在本教程中,我们将深入探讨WebWork2的核心概念、功能及其在实际开发中的应用。 WebWork2是Struts的前身,...
根据提供的文件信息,我们可以推断出这是一篇关于Java私塾中的Spring框架讲解与WebWork2整合教程的文章。下面将围绕这些关键词展开详细的讲解。 ### Spring框架基础 #### Spring简介 Spring是一个开源框架,最初由...
4. OGNL(Object-Graph Navigation Language):WebWork使用OGNL作为表达式语言,用于在Action和视图之间传递数据。OGNL允许开发者方便地访问和修改对象属性,增强了视图的动态性。 三、主要功能 1. 参数绑定:...
**Webwork2 开发指南** Webwork2 是一个基于Java的开源MVC(Model-View-Controller)框架,专门用于构建动态、交互式的Web应用程序。它提供了强大的数据绑定、动作控制、异常处理以及国际化等功能,使得开发者能够...
Webwork2是一个基于Java的MVC(模型-视图-控制器)框架,它在Web应用程序开发中提供了一种组织和管理代码的方式。以下是对Webwork2框架的学习总结: 1. **JAR包下载与项目配置**: - 开始学习Webwork2时,首先需要...
3. **与 Web 框架集成:** WebWork2 和 Struts2.x 等框架采用了 OGNL 作为默认的表达式语言来替代传统的 EL(Expression Language)。这不仅提高了开发效率,还简化了表单字段的处理过程。 **应用示例:** - 在 ...
WebWork 1 还引入了OGNL(Object-Graph Navigation Language)作为默认的数据绑定语言,使得在视图和模型之间传递数据变得更加方便。 WebWork 2 是WebWork 1 的后续版本,它引入了许多增强和新特性,如增强的动作...
WebWork2是一款基于Java的开源MVC(Model-View-Controller)框架,它为构建企业级Web应用程序提供了强大的支持。这个“webwork2官方文档中文版”是针对开发者的重要参考资料,帮助他们理解和掌握WebWork2的核心概念...
在探讨Struts2与WebWork2的联系与区别的过程中,我们不得不提到它们的历史渊源以及在Java Web开发领域中的地位。Struts2框架实际上可以视为WebWork2框架的继承者,两者之间的关系紧密而复杂,下面将从多个角度深入...