Child pages
  • OGNL Basics

Versions Compared


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


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:

Code Block

#foo.blah // returns foo.getBlah()
#bar.blah // returns bar.getBlah()
blah      // returns foo.getBlah() because foo is the root


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:

Code Block

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:

Code Block

[0].name   // call to animal.getName()
[1].name   // call to person.getName()

Wiki MarkupWith expression like \ [0\] ... \ [3\] etc. WebWork Struts 2 will cut the stack and still returned return back a CompoundRoot object. To get the top of that particular stack cut, use [0].top

ognl expression


<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ecb52625-190e-4c58-828f-45b988d0686b"><ac:plain-text-body><![CDATA[ [0].top

would get the top of the stack cut starting from element 0 in the stack (similar to top in this case)


<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a60fc314-7849-4ffb-8343-909ea5809aee"><ac:plain-text-body><![CDATA[ [1].top

would get the top of the stack cut starting from element 1 in the stack]]></ac:plain-text-body></ac:structured-macro>

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

By default, Struts 2 is configured to disallow this--to enable OGNL's static member support you must set the struts.ognl.allowStaticMethodAccess constant to true via any of the Constant Configuration methods.

OGNL's static access looks like this:

Code Block


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:

Code Block

<at:var at:name="vs" />FOO_PROPERTY
<at:var at:name="vs" />someMethod()

<at:var at:name="vs1" />FOO_PROPERTY
<at:var at:name="vs1" />someMethod()

<at:var at:name="vs2" />BAR_PROPERTY
<at:var at:name="vs2" />someOtherMethod()


Differences from the WebWork 1.x EL

Wiki MarkupBesides 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





<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a94d7722-8645-49b1-8f12-6c112a5218c1"><ac:plain-text-body><![CDATA[





not directly supported, but #baz is similar

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8a71cbc8-6728-4067-8f77-d597d29b0a09"><ac:plain-text-body><![CDATA[


top .

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



<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6ed1908d-a051-40ab-a306-3c7835ae4e70"><ac:plain-text-body><![CDATA[

#action['foo'] or #action.foocurrent action getter (getFoo())

#parameters['foo'] or

request parameter ['foo'] (request.getParameter())


<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="eca3aa6a-daf2-4f7f-9ccd-27cd9787786d"><ac:plain-text-body><![CDATA[

#request['foo'] or

request attribute ['foo'] (request.getAttribute())


<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0c69b076-7a7a-4cc3-99f3-b3ebe809216a"><ac:plain-text-body><![CDATA[

#session['foo'] or

session attribute 'foo'


<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9dbf47d8-47f2-4463-afa5-42b439b9e541"><ac:plain-text-body><![CDATA[

#application['foo'] or

ServletContext attributes 'foo'


<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a4597d90-8f46-4bf1-815b-99c28ded006b"><ac:plain-text-body><![CDATA[

#attr['foo'] or

Access to PageContext if available, otherwise searches request/session/application respectively