Introduction to Struts2

Introduction to Struts2

Author: Ashish Garg

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.

The automation of data transfer and type conversion is one of the most powerful features of Struts 2. With the help of OGNL, the Struts 2 framework allows transfer of data onto more complex Java-side types like List, Map, etc.

Custom converters can also be developed to extend the type conversion mechanism and it can handle any data type, including user-defined types.



OGNL is the interface between the Struts 2 framework string-based HTTP Input and Output and the Java-based internal processing. For developers who are conversant with OGNL, it will substantially improve efficiency and reduce maintenance headaches.

The framework sets the OGNL context to be our ActionContext, and the value stack to be the OGNL root object. (The value stack is a set of several objects, but to OGNL it appears to be a single object.) Along with the value stack, the framework places other objects in the ActionContext, including Maps representing the application, session, and request contexts. These objects coexist in the ActionContext, alongside the value stack (our OGNL root).


0 comments:

Post a Comment