Versions Compared

Key

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

OGNL is the Object Graph Navigation Language (see http://wwwcommons.ognlapache.org/proper/commons-ognl/ 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 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

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

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

...

Code Block
xml
xml
titleReferencing an Action property

<s:property value="postalCode"/>

...

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["mySessionPropKey"]'myRequestPropKey']"/>

The ActionContext is also exposed to Action classes via a static method.

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
java
java
<c:set var="foo" value="bar" scope="request"/>
<s:textfield name="username" label="%{#request.foo}" />

Collections (Maps, Lists, Sets)

Dealing with Collections (Maps, Lists, and Sets) in 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

<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

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

Code Block
xml
xml

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

Code Block
xml
xml

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

...

Info
titleHow the expression works

The lambda expression is everything inside the square brackets. The #this variable holds the argument to the expression, which is initially starts at 11in the following example is the number 11 (the code after the square-bracketed lamba expression, #fib(11)).

Code Block
xml
xml

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

...