`
leng_cn
  • 浏览: 301899 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

WEBWORK OGNL Basics

阅读更多
OGNL is the Object Graph Navigation Language (see http://www.ognl.org/ for the full documentation of OGNL). Here, we will cover a few examples of OGNL features that co-exist with the framework. To review basic concepts, refer to OGNL Basics.

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).

                     |
                     |--application
                     |
                     |--session
       context map---|
                     |--value stack(root)
                     |
                     |--request
                     |
                     |--parameters
                     |
                     |--attr (searches page, request, session, then application scopes)
                     |

There are other objects in the context map. The diagram is for example only.

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.

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['myRequestPropKey']"/>
The ActionContext is also exposed to Action classes via a static method.

ActionContext.getContext().getSession().put("mySessionPropKey", mySessionObject);
Collections (Maps, Lists, Sets)
Dealing with Collections (Maps, Lists, and Sets) in the framework comes often, so here are a few examples using the select tag.

Syntax for list: {e1,e2,e3}. This idiom creates a List containing the String "name1", "name2" and "name3". It also selects "name2" as the default value.

<s:select label="label" name="name" list="{'name1','name2','name3'}" value="%{'name2'}" />
Syntax for map: #{key1:value1,key2:value2}. This idiom creates a map that maps the string "foo" to the string "foovalue" and "bar" to the string "barvalue":

<s:select label="label" name="name" list="#{'foo':'foovalue', 'bar':'barvalue'}" />
To determine if an element exists in a Collection, use the operations in and not in.

<s:if test="'foo' in {'foo','bar'}">
   muhahaha
</s:if>
<s:else>
   boo
</s:else>

<s:if test="'foo' not in {'foo','bar'}">
   muhahaha
</s:if>
<s:else>
   boo
</s:else>
To select a subset of a collection (called projection), use a wildcard within the collection.

? - All elements matching the selection logic
^ - Only the first element matching the selection logic
$ - Only the last element matching the selection logic
To obtain a subset of just male relatives from the object person:

person.relatives.{? #this.gender == 'male'}
Lambda Expressions
OGNL supports basic lamba expression syntax enabling you to write simple functions.

(Dedicated to all you math majors who didn't think you would ever see this one again.)

Fibonacci: if n==0 return 0; elseif n==1 return 1; else return fib(n-2)+fib(n-1);
fib(0) = 0
fib(1) = 1
fib(11) = 89

分享到:
评论

相关推荐

    WebWork教程-表达式与言EL和OGNL- babydavic(王汉祥)的专栏- CSDNBlog.zip

    WebWork教程-表达式与言EL和OGNL- babydavic(王汉祥)的专栏- CSDNBlog.zipWebWork教程-表达式与言EL和OGNL- babydavic(王汉祥)的专栏- CSDNBlog.zip

    webWork

    4. OGNL(Object-Graph Navigation Language):WebWork使用OGNL作为表达式语言,用于在Action和视图之间传递数据。OGNL允许开发者方便地访问和修改对象属性,增强了视图的动态性。 三、主要功能 1. 参数绑定:...

    STRUTS2+ognl

    本文将详细介绍Struts2、Webwork以及Ognl的相关知识点,并提供入门指导。 **Struts2框架** Struts2是在原有的Struts1基础上发展起来的,它整合了Webwork框架的优势,提供了更灵活的控制流和更强大的拦截器机制。...

    OGNL教程,简单,看完懂OGNL语言

    随着时间的发展,OGNL逐渐演变为处理复杂数据关系的语言,广泛应用于多个知名Web框架,如WebWork(Struts2的前身)和Tapestry。 OGNL的语法简洁而强大,其基本单位是“导航链”,它可以用来访问对象的属性、调用...

    webwork用到的jar包

    3. **OGNL(Object-Graph Navigation Language)**:WebWork使用OGNL作为表达式语言,用于在视图和模型之间传递数据。OGNL支持丰富的语法,可以方便地访问和修改对象属性。 4. **类型转换(Type Conversion)**:...

    webwork2.1.7

    4. **OGNL(Object-Graph Navigation Language)**:WebWork使用OGNL作为表达式语言,用于在视图层和模型层之间传递数据,提供了强大的对象导航和访问能力。 5. **异常处理**:WebWork提供了一套统一的异常处理机制...

    webwork2开发指南

    4. OGNL(Object-Graph Navigation Language):WebWork2使用OGNL作为默认表达式语言,用于在视图层和模型层之间传递数据。 二、WebWork2快速入门 1. 安装配置:首先,需要在项目中引入WebWork2的依赖库,配置web....

    webwork 2.1.7.jar

    3. **Object-Graph Navigation Language (OGNL)**:WebWork使用OGNL作为表达式语言,用于在视图层和模型层之间传递数据。开发者可以用OGNL轻松访问和修改对象的属性,使得视图和模型之间的交互更为灵活。 4. **...

    OGNL百度百科OGNL百度百科

    3. **与 Web 框架集成:** WebWork2 和 Struts2.x 等框架采用了 OGNL 作为默认的表达式语言来替代传统的 EL(Expression Language)。这不仅提高了开发效率,还简化了表单字段的处理过程。 **应用示例:** - 在 ...

    webwork的jar包

    WebWork 1 还引入了OGNL(Object-Graph Navigation Language)作为默认的数据绑定语言,使得在视图和模型之间传递数据变得更加方便。 WebWork 2 是WebWork 1 的后续版本,它引入了许多增强和新特性,如增强的动作...

    WebWork文档(PDF)

    WebWork使用Ognl作为表达式语言,用于在Action和视图之间绑定数据,以及在Action中调用方法。Ognl提供了一种强大的方式来访问和操作对象属性,增强了模板语言的功能。 5. **配置与注解** WebWork支持XML配置文件...

    WebWork 教程

    随着版本演进,WebWork经历了重大变革,最新的WebWork2.x实际上是基于Xwork构建的,Xwork作为底层核心,专注于Command模式的实现,提供了诸如拦截器机制、表单验证、类型转换、OGNL表达式语言以及IoC容器等功能。...

    webwork-1.4-src.zip_webwork_webwork s_webwork.zip_webwork1.4.zip

    WebWork 是一个基于Java的开源MVC(Model-View-Controller)框架,它主要用于构建企业级的Web应用程序。WebWork1.4是该框架的一个较早版本,它为开发者提供了强大的功能,包括动作映射、数据绑定、异常处理、国际化...

    WebWork教程

    WebWork是一个由OpenSymphony组织开发的Java Web框架,它遵循MVC(Model-View-Controller)设计模式,并且特别注重组件化以及代码重用。WebWork2.x版本的发展前身是Rickard Öberg开发的WebWork,并且已经被拆分为...

    Ognl参考手册pdf

    - **Web组件与底层模型对象间的绑定语言**:如Web OGNL、Tapestry、WebWork和WebObjects等框架中都有广泛应用。 - **替代Jakarta Commons BeanUtils或JSTL EL的更强大语言**:相比于这些工具,OGNL提供了更为丰富的...

    OpenSymphony公司的ognl资源包

    在Java项目中,OGNL通常与其他OpenSymphony的框架一起使用,如WebWork(后来演变为Struts2的一部分),这些框架利用OGNL的特性来简化数据操作和提高开发效率。例如,在Struts2框架中,OGNL作为默认的表示层语言,...

    webwork所需要的包

    3. **表达式语言**:WebWork可能使用某种表达式语言(EL),如OGNL(Object-Graph Navigation Language),用于在视图层与模型数据交互。`ognl.jar`包含了OGNL的实现,使得开发者可以通过简洁的语法访问和操作对象...

    webwork2.2.4包

    4. **Ognl表达式语言(OGNL)**:WebWork使用OGNL作为默认的数据绑定和表达式语言,允许开发者在视图和模型之间灵活地传递和操作数据。 5. **异常处理(Exception Handling)**:WebWork提供了一种优雅的方式来处理...

    webwork2实例源代码

    7. **Ognl(Object-Graph Navigation Language)**:WebWork2使用OGNL作为表达式语言,用于在Action、视图和模型之间传递数据。OGNL能方便地访问和修改对象属性,以及执行动态计算。 8. **异常处理**:WebWork2提供...

Global site tag (gtag.js) - Google Analytics