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.

valueStack.push(context);

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.
index.jsp

<%@ 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> 
 </s:form> 

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
     <filter-name>struts2</filter-name>
     <filter-class> 
       org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 
     </filter-class>
   </filter>
   <filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

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

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) {
 this.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"));
 
                stack.push(context);
                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
struts.xml

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

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

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.
success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  <%@ taglib prefix="s" uri="/struts-tags" %>

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

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.

Summary

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