Struts 2 Value Stack/OGNL - Apache Struts 2

what is Value Stack/OGNL in Struts2?

The Value Stack

The value stack is said to be a series of several objects which keeps in the following order:
SN Objects & Description
1 Temporary Objects
There are various temporary objects which are created during execution of a page. For example the current iteration value for a collection being looped over in a JSP tag.
2 The Model Object
If you are using model objects in your struts application, the current model object is placed before the action on the value stack
3 The Action Object
This will be the current action object which is being executed.
4 Named Objects
These objects include #application, #session, #request, #attr and #parameters and refer to the corresponding servlet scopes
These objects include #application, #session, #request, #attr and #parameters and refer to the corresponding servlet scopes
The value stack can be accessed with the tags provided for JSP, Velocity or Freemarker. There are various tags which we will study in separate chapters, are used to get and set struts 2.0 value stack. You can get valueStack object inside your action as follows:
Once you have a ValueStack object, you can manipulate with the following methods
SN ValueStack Methods & Description
1 Object findValue(String expr)
Find a value by evaluating the given expression against the stack in the default search order.
2 CompoundRoot getRoot()
Get the CompoundRoot which holds the objects pushed onto the stack.
3 Object peek()
Get the object on the top of the stack without changing the stack.
4 Object pop()
Get the object on the top of the stack and remove it from the stack.
5 void push(Object o)
Put this object onto the top of the stack.
6 void set(String key, Object o)
Sets an object on the stack with the given key so it is retrievable by findValue(key,...)
7 void setDefaultType(Class defaultType)
Sets the default type to convert to if no type is provided when getting a value.
8 void setValue(String expr, Object value)
Attempts to set a property on a bean in the stack with the given expression using the default search order.
9 int size()
Get the number of objects in the stack.

The OGNL

The Object-Graph Navigation Language (OGNL) is known as the powerful expression language that helps in referencing and manipulating data on the Value Stack. OGNL also helps in data transfer and type conversion.
The OGNL is same as that of the JSP Expression Language. OGNL mainly works on the idea of having a root or default object within the context. The properties of the default or root object can be referred with the help of the mark up notation, which is the pound symbol.
As mentioned earlier, OGNL is based on a context and Struts builds an ActionContext map to make use with OGNL. The Action Context map consists of the following:
  • application - application scoped variables
  • session - session scoped variables
  • root / value stack - all your action variables are stored here
  • request - request scoped variables
  • parameters - request parameters
  • atributes - the attributes stored in page, request, session and application scope
It is very necessary to understand that the Action object is always available in the value stack. Hence, if your Action object has properties of both x and y, it is very easy and ready to use by the user.
Objects in the ActionContext are referred by using the pound symbol; however, the objects in the value stack can be referred directly, for example if employee is a property of an action class then it can be referenced as follows:
instead of
If you have an attribute in session called "login" you can retrieve it as follows:
OGNL also supports dealing with collections - namely Map, List and Set. For example to display a dropdown list of colors, you could do:
The OGNL expression looks very smart interpreting the colours as "red","yellow","green" and builds a list based on that colours.
The OGNL expressions will be used extensively in the next chapters when we will study different tags. So rather than looking at them in isolation, let us look at it using some examples in the Form Tags / Control Tags / Data Tags and Ajax Tags section.

ValueStack/OGNL Example

Create Action:

Here you can see the following action class where accessing value Stack takes place and with the use of OGNL we will try to set few keys that will be accessed in the view i.e JSP page.
Actually, when the action is executed Struts 2 places your action at the top of the valueStack. So, the usual way to put stuff on the Value Stack is to add getters/setters for the values to your Action class and then use <s:property> tag to access the values. Here to show you how exactly ActionContext and ValueStack work in struts is as follows-

Create Views

Let us create the below jsp file HelloWorld.jsp in the WebContent folder in your eclipse project. This view will be displayed in case action returns success:
We also need to create index.jsp in the WebContent folder whose content is as follows:

Configuration Files

Following is the content of struts.xml file:
Following is the content of web.xml file:
To create a War File right click on the project name and click Export > WAR File. Then deploy this WAR in the Tomcat's webapps directory. Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/index.jsp. This will give you following screen:
helloworldstruts 4(1)
Now you can see the above screen where you need to enter any word in the given text box and click "Say Hello" button to execute the defined action. Now check the log generated, you will find following text at the bottom:
Here you can see in the above screen, where the value is displayed which ever value entered by the user, the value of key1 and key2 which we had put on ValueStack earlier.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Apache Struts 2 Topics