Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Overview

OGNL is the Object Graph Navigation Language - (see http://wwwcommons.ognlapache.orgImage Removed/proper/commons-ognl/ for the full documentation of OGNL). In this document Here, we will only show cover a few examples of OGNL features that co-exist with Webworkthe framework. To review basic concepts, refer to OGNL Basics

...

.

The framework

Webwork with OGNL

Webwork uses a standard naming context to evaluate OGNL expressions. The top level object dealing with OGNL is a map Map (usually referred as a context map or context). OGNL has a concept notion of there being 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(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).

No Format
No Format

                     |--request
                     |--application
                     |--application
                     |--session
       context map---|--OgnlValueStack
                     |--value stack(root)
                     |
                     |--sessionaction (the current action)
                     |
                     |--attrrequest
                     |
                     |--parameters
                     |
                     |--attr (searches page, request, session, then application scopes)
                     |

(info) There Note that their are other objects in the context map, I'm just referring to a few for this example. Now, your actions instances are placed in the OGNLValueStack so you can refer to your bean properties without the #. 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.

Code Block
xml
xml
titleReferencing an Action property
<s:
<ww: property value="myBean.myPropertypostalCode"/>

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

Code Block
xml
xml
1title:Reference Another Object in the ActionContext
<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.For sessions,request, and the rest that lie in the context map:

Code Block
java
java

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

You can also put expression for attributes that don't support dynamic content, like below:

Code Block
xmljavaxml
java

<ww<c:set propertyvar="foo" value="#session.mySessionPropKeybar"/> or
<ww: property value="#session['mySessionPropKey'] scope="request"/> or
<ww<s:textfield property valuename="username" label="#attr.mySessionPropKey"%{#request.foo}" />

Collections (Maps, Lists, Sets)

Dealing with collectionsCollections (mapsMaps, listsLists, and setsSets) in webwork the framework comes often, so here below please there are a few examples using the select tag:. The OGNL documentation also includes some examples.

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.

Code Block
xml
xml

<webwork<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":

Code Block
xml
xml

<webwork<s:select label="label" name="name" list="#{'foo':'foovalue', 'bar':'barvalue'}" />

You may need to To determine if an element exists in a collection. You can accomplish this with Collection, use the operations in and not in.

Code Block
xml
xml

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

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

To select a subset of a collection (called projection), you can 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:

Code Block
xml
xml

person.relatives.{? #this.gender == 'male'}

Lambda Expressions

OGNL supports basic lamba expression syntax enabling you to write simple functions.

For example:

For (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

Info
titleHow the expression works

The lambda expression is everything inside the square brackets. The #this variable holds the argument to the expression, which

...

in the following example is the number 11 (the code after the square-bracketed lamba expression, #fib(11)).

Code Block
xml
xml

<ww<s:property value="#fib =:[#this==0 ? 0 : #this==1 ? 1 : #fib(#this-2)+#fib(#this-1)], #fib(11)" />

Next: Tag Syntax