OGNL in Struts2 Tutorial

OGNL(Object-Graph Navigation language) is an expression language inherited by Struts from WebWork. It is used in struts to access model objects from a jsp page. OGNL can be used to bing 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,convert one type to another.

Value Stack is an object created prior to any action method is executed and is used to store actions and other objects. The struts framework stores the value stack in request attribute struts.valueStack and is used by jsp to display action and other info. Value Stack contains Object Stack and the Context Map. Action and other objects are pushed ito the Object Stack where as maps(parameters,request,session,application,attr) are stored in 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 [0].message returns the message property valueof the object on top. [1]duration returns the duration property of the second object.

Methods and fields can be accessed through OGNL. e.g @java.util.Calendar@DECEMBER is used to access the static property DECEMBER in Calendar class. To call a atatic function we use @pac1.pac2.Util@now(); if we have a static function called now() in the Util class. To call a nonstatic field or function we use. object.fieldname or we can use [0].datepattern where [0] refers to the first object in the value stack.

Leave a Reply