• October 3, 2014
  • Struts2
  • Comments Off on Value Stack in struts 2

Value Stack in struts 2

Valuestack in struts2 pdf download.

In struts 2 ValueStack represents a stack that contains the references of application specific objects. When a request come an object of ValueStack is created first and after it object of Action, Model or any other java resources are created. The references of all these objects are maintained in ValueStack.

Note: The objects in the ValueStack are available to the response on UI pages. ValueStack uses OGNL for this process.

Main parts of ValueStack:

  1. 1. Object Stack: It is used to contain the references of the Objects.
  2. 2. Context Map: It contains the maps like Request Parameters, Request Attributes, Session Attributes and Application Attributes.

Commonly used methods of ValueStack interface:

1. findValue(String expr):Find a value by evaluating the specified expression.

Syntax: public Object findValue(String expr)

2. findString(String expr):Find a string by evaluating the specified expression.

Syntax: public String findString(String expr)

3. peek(): It gives the object which is on the top of the stack and not remove it from the stack.

Syntax: public Object peek()

4. pop(): It gives the object which is on the top of the stack and remove it from the stack.

Synatx: public Object pop()

5. push(Object o):Put the specified object on the top of the stack.

Syntax: public void push(Object o)

6. set(String key, Object value):Sets an object on the stack with the specified key. It can retrieve using findValue(key).

Syntax: public void set(String key, Object value)

7. size(): It gives the no. of objects in the stack.

Syntax: public int size()



<%@ taglib uri="/struts-tags" prefix="s"%>
		<title>Struts 2 ValueStack example</title>
		<h3>This is a ValueStack example.</h3>
		<s:form action="welcome">
		 <s:textfield name="userName" label="UserName" />
		 <s:submit value="Hello" align="center"/>


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 


"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	<package name="user" extends="struts-default">
		<action name="welcome" 
			<result name="success">/welcome.jsp</result>


import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
 * This class is used as an action class.
 * @author javawithease
public class Login {
	//data members
	private String userName;
	//business logic
	public String execute(){
		ValueStack stack = 
		Map<String, Object> context = 
			new HashMap<String, Object>();
	  	context.put("value1", new String("First value")); 
	  	context.put("value2", new String("Second value"));
		return "success";	
	//getter setters
	public String getUserName() {
		return userName;
	public void setUserName(String userName) {
		this.userName = userName;


<%@ taglib uri="/struts-tags" prefix="s"%>
		<title>Struts 2 ValueStack example</title>
		<h3>This is a ValueStack example.</h3>
		<h5>UserName: <s:property value="userName" /></h5>
		<h5>Value1: <s:property value="value1" /></h5>
		<h5>Value2: <s:property value="value2" /></h5>


struts 7 login
Enter username.
struts 7 value
Click on Hello button.
struts 7 final
Download this example.
Next Topic: Interceptors in struts 2 with example.
Previous Topic: OGNL in struts 2 with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Comments are closed.