Monday, 17 November 2014

Core Components of Struts ValueStack,OGNL,ActionInvocation

Struts 2 ValueStack Tutorial

A valueStack is simply a stack that contains application specific objects such as action objects and other model object.
At the execution time, action is placed on the top of the stack.
We can put objects in the valuestack, query it and delete it.

ValueStack Interface

The struts
 2 framework provides an interface to deal with valuestack. It provides many useful methods.

Methods of ValueStack interface

There are many methods in ValueStack interface. The commonly used methods are as follows: 
public String findString(String expr) finds the string by evaluating the given expression.
public Object findValue(String expr) finds the value by evaluating the specified expression.
public Object findValue(String expr, Class c) finds the value by evaluating the specified expression.
public Object peek() It returns the object located on the top of the stack.
public Object pop() It returns the object located on the top of the stack and removes it.
public void push(Object o) It puts the object on the top of the stack.
public void set(String key, Object value) It sets the object on the stack with the given key. It can be get by calling the findValue(key) method.
public int size() It returns the number of objects from the stack.


Struts 2 ActionContext

The ActionContext is a container of objects in which action is executed. The values stored in the ActionContext are unique per thread (i.e. ThreadLocal). So we don't need to make our action thread safe. 
We can get the reference of ActionContext by calling the getContext() method of ActionContext class. It is a static factory method. For example: 
  1. ActionContext context = ActionContext.getContext(); 


Struts 2 ActionInvocation

The ActionInvocation represents the execution state of an action. It holds the action and interceptors objects.

ActionInvocation Interface

The struts
 framework provides ActionInvocation interface to deal with ActionInvocation. It provides many methods, some of them can be used to get the instance of ValueStack, ActionProxy, ActionContext, Result etc.

Methods of ActionInvocation Interface

The commonly used methods of ActionInvocation interface are as follows:
No.MethodDescription
1)public ActionContext getInvocationContext()returns the ActionContext object associated with the ActionInvocation.
2)public ActionProxy getProxy()returns the ActionProxy instance holding this ActionInvocation.
3)public ValueStack getStack()returns the instance of ValueStack.
4)public Action getAction()returns the instance of Action associated with this ActionInvocation.
5)public void invoke()invokes the next resource in processing this ActionInvocation.
6)public Result getResult()returns the instance of Result.



Struts 2 OGNL

The Object Graph Navigation Language (OGNL) is an expression language. It simplifies the accessibility of data stored in the ActionContext.
The struts
 framework sets the ValueStack as the root object of OGNL. Notice that action object is pushed into the ValueStack. We can direct access the action property.
  1. <s:property value="username"/>  
Here, username is the property key.
The struts framework places other objects in ActionContext also e.g. map representing the requestsession,application scopes.
To get these values i.e. not the action property, we need to use # notation. For example to get the data from session scope, we need to use #session as given in the following example:
  1. <s:property name="#session.username"/>  
(or) <s:property name="#session['username']"/>

No comments:

Post a Comment