Struts - SPLessons

Chapter 17

Struts 2 Value Stack

Struts 2 Value Stack

Struts 2 Value Stack is a type of stack. ValueStack is used for hiding some data internally. Struts 2 Value Stack, will be executed in execute() method. It contains some objects, methods and request values. Struts 2 Value Stack contains some objects like temporary objects, model objects, action objects, named objects.

Create the ValueStack objects through getValueStack() method. after creating the object, one need to add hiding the data using some methods.

Method Description
Object findValue(String expr) find a value by evaluating the given expressionagaint the stack in the default search order.
CompoundRoot getRoot() Using the getRoot() which holds the objects pushed onto the stack.
Object peek() Get the object on the top of the stack without changing the stack.
Object pop() Get the object on the top of the stack and remove it from the stack.
Void push(Object 0) Put this object onto the top of the stack.
Void set(String Key,Object 0) Sets an object on the stack with the given key so it is retrievable by findvalue(key…)
int size() Get the number of objects present in valuestack objects.

If one hide some data, That data will be pushed into stack object, after getting the values through getValueStack(). ActionContext interface provides the getValueStack() object. After creating the ValueStack object, one need to create the collection object, then need to add internal data in Collection object. Collection object is pushed into ValueStack object.

  // creating value stack object

ValueStack valueStack = ActionContext.getContext().getValueStack();

// createing the collection object.

Map<String, Object> context = new HashMap<String, Object>;

context.put("key1", new String(" value1"));
context.put("key2", new String(" value2"));

// push the collection object in valuestack object.


For example, create an application. In this application, provide id details and hide the name and salary details in value stack object.

Create the project directory structure.

Add the jar files in lib folder like Struts2 jar files and servlet-api.jar.

Create the View page and forward the request to Action class from browser page.

<%@ taglib uri="/struts-tags" prefix="s" %> 
 <s:form action="employee"> 
    <s:textfield name="id" label="Employee Id"></s:textfield> 
    <s:submit value="submit"></s:submit> 

Here the developer ceated textfield to enter the Employee ID and also created Submit button.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns:web="" xsi:schemaLocation="">

The StrutsPrepareAndExecuteFilter is a combination of StrutsPrepareFilter and StrutsExecuteFilter. Make sure that filter-name should be same.

package com.itoolsinfo;

import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;

public class Employee 
 private int id;

public int getId() {
 return id;

public void setId(int id) { = id;

public String execute()
   ValueStack stack = ActionContext.getContext().getValueStack();
    Map<String, Object> context = new HashMap<String, Object>();
          context.put("name", new String(" Joshaf "));
          context.put("salary", new String(" 40000"));
                return "success";

The ActionContext is a container of objects in which action is executed. The values stored in the ActionContext are unique per thread. So don’t need to make our action thread safe. One can get the reference of ActionContext by calling the getContext() method of ActionContext class

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "">
   <package name="default" extends="struts-default">

      <action name="employee" class="com.itoolsinfo.Employee" method="execute">
           <result name="success">success.jsp</result>

Where success is a predefined result type. action element is the sub component of package. It speaks to an activity to be conjured for the approaching request. It has name, class and method attributes. In the event that you don’t determine name property as a matter of course execute() technique will be summoned for the predetermined actiion class.

It give the response into success page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
  <%@ taglib prefix="s" uri="/struts-tags" %>

       ItoolsInfo Employee Details successfully saved.
       Employee Id : <s:property value="id"/>
       Employee Name : <s:property value="name" />
       Employee Salary : <s:property value="salary" /> 

One have to deploy the application in Tomcat\webapps folder. Run the application and the output will be displayed in a browser.
Enter the employee id and click on submit button.


  • The Struts 2 Value Stack will contain objects regarding an application.
  • The Struts 2 Value Stack can be defined as collection of more objects.