Thursday, 7 November 2013

OGNL Implementation in Struts

Struts 1.x

Struts 1.x used expression language(EL) which uses JSTL as its base.  Struts-EL tag classes are subclasses of Struts tag classes. The EL has basic object graph traversal, but it is not very powerful  and also the  indexed property support is very weak.

Struts 2

Struts 2 was released in 2007 with many exciting features. As compared to Struts 1.x, it simplified the app development task by automating data transfer(form beans to data beans and vice versa as in Struts 1.x) and type conversion(parsing string into double/integer along with exception resolution was to be done in Struts 1.x).


With Object Graph Navigation Language(OGNL) in Struts 2, data can be transferred with complex data structures like List and Map. User-defined types can be used with the help of custom converters, which are quite easy to write. OGNL acts as a layer between Struts 2 framework and Java-based processing unit.


OGNL greatly relieves developer from extra coding and maintenance effort. OGNL binds Java-side data directly to the corresponding fields in view layer. Built-in data converters save conversion work while data passes to or from Java environment. Field names in HTML can be generated using OGNL expressions to bind them directly to corresponding Java property and thus eliminates redundant code in Action classes. On contrary to the standard JSP mechanism for binding objects into the page context for access in Struts 1.x, Struts 2 uses ValueStack technology by which  taglibs can access values without coupling view to the object type it is rendering. ValueStack is set as OGNL’s root object. It contains application specific objects like action and also the model objects.

Context Map

OGNL context is set to ActionContext. ActionContext is a container of objects in which action is executed. We get a reference of it by simply calling ActionContext.getContext(). There are other objects in ActionContext like Maps(referred to as context or context map) to represent application, session and request contexts.

Data Access

The root object is referred simply by its name, without any special symbol prefixing it. Since Action instance is always pushed on ValueStack, which is the OGNL root, references to Action properties can omit pound sign. But for rest of the objects in ActionContext, ‘#’ has to be used.
Example: To refer to Action property:

<s:property value=”firstName”/>

For other objects:

<s:property value=”#session.username”/>   Or

<s:property value=”#session[‘username’]/>

Similarly we can refer to properties of request, application or attr (attribute in scope).
Collections can also be referred using OGNL.List is referred by {value1, value2,....}

<s:select label=”Continent” list=”{‘Asia’, ‘Europe’, ‘Africa’} value=”defaultContinent”/>

Alternatively, the list can be populated in Action class with its getter, setter provided.

Map is referred by #{key: value ,....}

<s:select label=”Continent” list=”#{‘first’:‘Asia’, ‘second’:‘Europe’,’third’: ‘Africa’} value=”defaultContinent”/>

In case of Set, we can check whether an item exists in it or not using ‘in’ or ‘not in’.

<s:if test=”’Asia’ in {‘Asia’, ‘Europe’, ‘Africa’}
        Exists in the set

Percent (%) symbol

It is used to force OGNL expression evaluation, which results in querying ValueStack for the property.

<s:property name=”%{continent}”/>  (‘#’ accesses named ValueStack property)

At (@) symbol

It is used to refer static properties and method. We need to enable it in properties file by setting-

and then access like this-

<s:property value="@com.test.TestClass@STATIC_PROP" />

Dollar ($) symbol

Used in JSTL expressions.

‘OGNL’ name might have sounded like something difficult to first time readers. But I’m sure, after reading this post you must be ready to shake hand with OGNL!!

No comments:

Post a Comment