OGNL in Struts 2
OGNL(Object-Graph Navigation language) is an expression language inherited by Struts from WebWork.
Use of OGNL
- OGNL is used in struts to access model objects from a jsp page.
- It can be used to bind GUI elements to model objects in struts framework.
- It can be used to invoke methods of the model.
- It can create lists,maps to be used with GUI.
- Bind generic tags with model objects.
Value Stack is an object created prior to any struts action method is executed and is used to store actions and other objects. The struts framework stores the value stack in request attribute named “struts.valueStack” and is used by jsps to display action and other info.
Value Stack contains two logical units, the Object Stack and the Context Map.
Action and other objects are pushed into the Object Stack where as maps (parameters, request, session, application, attr) are stored in Context Map.
Maps in the Context Map
- parameters. Is a map containing the parameters in the current request.
- request. Is a map containing attributes in the current request.
- session. Is a map containing the session attributes for the current user.
- application. Is a map containing the ServletContext attributes.
- attr. Is a map that searches for attributes in the the order: request, session, application.
OGNL can be used to access objects in the Object Stack and the Context Map. To access Context Map properties we use a # in front of the OGNL expression, if not used search will take place from Object Stack.
eg #session.code returns the value of the session attribute code.
Accessing Object Stack object properties
.message,[“message”],or [‘message’] returns the message property value of the object on top.
.duration,.[“duration”],or [‘duration’] returns the duration property of the second object.
To print the duration property of the first stack object we can use <s:property value=”.duration”/>
Searching of property in Object Stack
.message–>starts searching for the message property from the 1st object in stack if not found searches in the next object at index position and goes on.
.message is same as writing only message. In both cases the searching starts from the 1st object in the Value Stack.
Reading Object Properties in the Context Map
To access properties of Objects in the Context Map we can use any of the following forms.
The folowing expression returns the firstName property of an employee object stored as the request attribute.
The following expression will search for the lastAccessedTime attribute in the request object. If it doesn’t find in the request will search in the session and consequently in the application object.
Accessing Methods an Fields using OGNL
Methods and fields can be accessed through OGNL. e.g @[email protected] is used to access the static property DECEMBER in Calendar class. To call a static function we use @[email protected](); if we have a static function called now() in the Util class in pac1.pac2 package.
To call a nonstatic field or function we use. object.fieldname or we can use .datepattern where  refers to the first object in the value stack.