Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
ValueStack
3.
Methods to Manipulate ValueStack
4.
The OGNL
5.
ValueStack/OGNL Example
5.1.
Create Action
5.2.
Create Views
5.3.
Creating Configuration Files
6.
Frequently Asked Questions
6.1.
What is ValueStack?
6.2.
What is OGNL?
6.3.
Why are Struts used?
6.4.
What configuration files are used in Struts?
6.5.
What is Struts XML?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Value Stack / OGNL

Introduction 

As a developer, we sometimes need a container that can store, reference and manipulate the actions, action objects and other model objects at the time of execution of the application.

struts coding ninjas

In this article, we will discuss ValueStack and OGNL, which are used to store and reference and manipulate the actions, action objects and other model objects, respectively, at the time of execution of the application.

ValueStack

A ValueStack is a stack that holds application-specific items like action objects and other model objects. Action is pushed at the top of the stack during execution. We can store items in the ValueStack, query them, and remove them. You may obtain a valueStack object within your action by doing the following.

ActionContext.getContext().getValueStack()


The value stack is a collection of objects that holds the following items in the specified sequence.

struts coding ninjas

Methods to Manipulate ValueStack

Once you have a ValueStack object, you may alter it using the methods shown below.
 

struts coding ninjas

The OGNL

The Object-Graph Navigation Language (OGNL) is a sophisticated expression language for referring to and manipulating data on the ValueStack. OGNL also aids data transport and type conversion. The OGNL and the JSP Expression Language are pretty similar. The goal of OGNL is to have a root or default object within the context. The markup notation, the pound sign, may refer to the attributes of the default or root object. 

For example, if the employee is a property of an action class, then it can be referenced as follows:

<s:property value = "name"/>


If you have a session attribute called "login," you may obtain it as follows.

<s:property value = "#session.login"/>


OGNL also allows working with collections, namely Map, List, and Set. You might, for example, perform the following to display a colour dropdown list:

<s:select name = "color" list = "{'red','yellow','green'}" />

ValueStack/OGNL Example

Let us discuss a complete example that shows the usage of ValueStack and OGNL. The steps involved in the sample are discussed below:

Create Action

Consider the following action class, in which we access valueStack and then set a few keys that we will access using OGNL in our view, i.e., the JSP page.

package com.codingninjas.struts2;


import java.util.*; 


import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class HelloWorldAction extends ActionSupport {
   private String name;


   public String execute() throws Exception {
      ValueStack stack = ActionContext.getContext().getValueStack();
      Map<String, Object> context = new HashMap<String, Object>();


      context.put("key1", new String("This is key1")); 
      context.put("key2", new String("This is key2"));
      stack.push(context);


      System.out.println("Size of the ValueStack: " + stack.size());
      return "success";
   }  


   public String getName() {
      return name;
   }


   public void setName(String name) {
      this.name = name;
   }
}

Create Views

Let's put the HelloWorld.jsp file in your eclipse project's WebContent folder. If the action is successful, this view will be displayed.

<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>


<html>
   <head>
      <title>Hello World</title>
   </head>
   
   <body>
      Entered value : <s:property value = "name"/><br/>
      Value of key 1 : <s:property value = "key1" /><br/>
      Value of key 2 : <s:property value = "key2" /> <br/>
   </body>
</html>


We must include index.jsp in the WebContent folder with the following information.
 

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


<html>
   <head>
      <title>Hello World</title>
   </head>
   
   <body>
      <h1>Hello World From Struts2</h1>
      <form action = "hello">
         <label for = "name">Please enter your name</label><br/>
         <input type = "text" name = "name"/>
         <input type = "submit" value = "Say Hello"/>
      </form>
   </body>
</html>

Creating Configuration Files

The following is the struts.xml file's content.

<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>
   <constant name = "struts.devMode" value = "true" />
   <package name = "helloworld" extends = "struts-default">


      <action name = "hello" 
         class = "com.tutorialspoint.struts2.HelloWorldAction" 
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>
      </action>


   </package>
</struts>


The following is the web.xml file's content.

<?xml version = "1.0" Encoding = "UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns = "http://java.sun.com/xml/ns/javaee" 
   xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id = "WebApp_ID" version = "3.0">
   
   <display-name>Struts 2</display-name>
   
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>


   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>


Finally, right-click the project's name and select Export > WAR File to generate a War file. After that, place this WAR in Tomcat's web apps directory, start Tomcat, and navigate to http://localhost:8080/HelloWorldStruts2/index.jsp.

This will result in the screen shown below.

struts coding ninjas


Insert any word in the provided text field and click the "Say Hello" button. Check the created log for the following text at the bottom.

Size of the ValueStack: 3

Frequently Asked Questions

What is ValueStack?

A ValueStack is a stack that holds application-specific items like action objects and other model objects. Action is pushed at the top of the stack during execution. We can store items in the ValueStack, query them, and remove them.

What is OGNL?

The Object-Graph Navigation Language (OGNL) is a sophisticated expression language for referring to and manipulating data on the ValueStack. OGNL also aids data transport and type conversion.

Why are Struts used?

Struts is an open-source framework that utilises a Model, View, Controller (MVC) architecture that extends the Java Servlet API. It allows you to build stable, extendable, and adaptable online applications using standard technologies like JSP pages, JavaBeans, resource bundles, and XML.

What configuration files are used in Struts?

xml File. The struts.xml file provides configuration information that you will edit when actions are created. This file may be used to alter an application's default settings, such as struts.

What is Struts XML?

The struts.xml file contains the configuration data that will be changed when actions are generated. DevMode = false, as well as other default parameters provided in property files, can be altered for an application that uses this file, such as struts.

Conclusion

In this article, we have extensively discussed ValueStack and OGNL with their example. 

Do you not feel eager to read/explore additional information on the subject of Struts 2 after reading about the ValueStack and OGNL? See the Struts 2 documentationStruts Tutorial for beginnersStruts 2 Intro, and 15 Best Java frameworks to use in 2022-Coding Ninjas.

You can also consider our Mern Stack Course to give your career an edge over others.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

coding ninjas

Live masterclass