WebWork 2 : OGNL
This page last changed on Dec 14, 2004 by casey.
OverviewOGNL is the Object Graph Navigation Language - see http://www.ognl.org for the full documentation of OGNL. In this document we will only show a few examples of OGNL features that co-exist with Webwork.
Webwork with OGNLWebwork 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). OGNL has a concept of a root object (in webwork terms, this is the OGNLValueStack). Along with the root, other objects are placed in the context map (referred as in the context) including your session/application/request/attr maps. These objects have nothing to do with the root, they just exist along side it in the context map. So, to access these objects, the # is used telling ognl not to look in the root object, but within the rest of the context | |--request | |--application | context map---|--OgnlValueStack(root) | |--session | |--attr | |--parameters <ww: property value="myBean.myProperty"/>
For sessions,request, and the rest that lie in the context map: ActionContext.getContext().getSession().put("mySessionPropKey", mySessionObject);
<ww: property value="#session.mySessionPropKey"/> or <ww: property value="#session['mySessionPropKey']"/> <ww: property value="#attr.mySessionPropKey"/> Collections (Maps, Lists, Sets)Dealing with collections(maps, lists, and sets) in webwork comes often, so here are a few examples using the select tag:Syntax for list: {e1,e2}. This creates a List containing the String "name1" and "name2". <webwork:select label="'lebal'" name="'nmae'" list="{'name1','name2'}" />
Syntax for map: #{key1:value1,key2:value2}. This creates a map that maps the string "foo" to the string "foovalue" and "bar" to the string "barvalue": <webwork:select label="'lebal'" name="'nmae'" list="#{'foo':'foovalue', 'bar':'barvalue'}" />
You may need to determine if an element exists in a collection. You can accomplish this with the operations in and not in <ui:if test="'foo' in {'foo','bar'}"> muhahaha </ui:if> <ui:else> boo </ui:else> <ui:if test="'foo' not in {'foo','bar'}"> muhahaha </ui:if> <ui:else> boo </ui:else>
To obtain a subset of just male relatives from the object person: person.relatives.{? #this.gender == 'male'} Lambda ExpressionsOGNL supports basic lamba expression syntax enabling you to write simple functions.For example:For all you math majors who didn't think you would ever see this one again. <ww:property value="#fib =:[#this==0 ? 0 : #this==1 ? 1 : #fib(#this-2)+#fib(#this-1)], #fib(11)" />
|
![]() |
Document generated by Confluence on Dec 14, 2004 16:36 |