OGNL & ValueStack in Struts 2 Framework
Introduction to OGNL
OGNL is the Object Graph Navigation Language which is an opensource framework from Apache Commons project and is used in setting and getting properties from the Java Beans. OGNL also enables to invoke the methods from the Java classes. OGNL was primarily designed to use with the UI forms to set the form values to the Java Beans.OGNL was integrated to struts 2 when the Webworks framework was integrated and a lot of hardwork was put to bring in the OGNL into the Struts 2 to make it simple to use for the developers.
Role of OGNL in Struts 2
OGNL helps in three areas in Struts 2 during the request handling process.
- OGNL offers the expression language which can be used in the forms and response pages.
- OGNL helps in the type conversion during setting the form values to the Java beans types.
- Helps in looking for objects in ValueStack.
OGNL and ValueStack:
OGNL and ValueStack work together in Struts 2 to handle a request. OGNL offers the expression language which can be part of form tags and UI pages. This expression language helps to map the entered values to the destination properties of the Java Bean to which they have to be set.
When a form is submitted, the values are transmitted as Strings through the HTTP Protocol. These String values have to be converted to the respective primitive or custom java types to set them in the beans. During the process of setting the values, OGNL consults the type converters available in Struts 2 and helps in conversion of the form values to the destination types and will set into the Java Beans.
Without the Type Converters, developers have to end up writing the Mapping Beans which pic the values from the form beans and then have to convert to the destination types. This leads to more code to handle a request and also puts more burden to test the code.
OGNL looks for the properties in the Objects that are loaded in the Valuestack and when found, the values are converted and set to Model objects.
The above image shows the flow of request and how OGNL and ValueStack handle the request.
I have segregated the whole OGNL into three different topics for a better understanding.
2. Type conversion.
3. Expression Language or Tags.
ValueStack in Struts 2
Understanding the concept and importance of ValueStack in Struts2 is primary importance to work with Struts 2 framework. ValueStack is the platform or playground for the Struts 2 framework for its request handling. Lets explore ValueStack in detail.
Consider a user has sent request to the Struts 2 application. First a ValueStack object is created for that request and then the objects like ACTION, Model and any Java resources that are needed are created. These objects references are maintained in the ValueStack.
Apart from the Objects created, we might also need the objects like request attributes, Session attributes and Application context to set and access the objects in those scopes. The ValueStack also gets the references to these objects as well.
So, if you consider ValueStack, then you should keep in mind that this object is like an address book or contact list for all the objects that are needed to handle this request.
One of the major advantages of Valuestack is that any object with which we work or the model objects are available to the response UI pages through the OGNL expression language. We do not need to set every object to an attribute map to access them in response as we do with the J2EE API
Object Stack and Context Map in ValueStack
ValueStack has two parts.
Object Stack is a Stack which stores the references to the Objects that are created to handle the request. The name itself speaks that it is a stack. The objects created are referenced in a stack and can be accessed using Indexes.
Context Map is a place where the Maps like Request Parameters, Request Attributes, Session Attributes and Application Attributes. The context map also has an extra map called attribute map. The attribute map helps to search through all the attributes starting from Request scope, session scope and then in the application scope. Where ever it finds the requested attribute first, it returns the value from that scope.
Accessing Object Stack with expression language:
Object stack properties can be accessed using the expression language as given below:
Any of the three expressions can be used in <s:property/> tag to access the objects on object stack. A normal way to access the object is to use the names of the beans instead of the index.
Accessing the context map using expression language:
Request Parameters are returned in the form of Array. So to retrieve a parameter, we need to know in which index a parameter is present.
<s:property value="#parameters.count"/> will fetch the first request parameter.
Given below are the expressions that can be used to access the request attributes.
Accessing the attributes in Session Scope:
Accessing the attributes in Application scope
Accessing attributes using attribute Map
Please note that all the expressions mentioned above should be part of the <s:property/> tag.