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.
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.
Methods to Manipulate ValueStack
Once you have a ValueStack object, you may alter it using the methods shown below.
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>
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.
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.