ValueStack and OGNL

ValueStack and OGNL

In Struts2,  ValueStack  and  OGNL  (Object-Graph Navigation Language) are integral parts of the framework’s data handling and expression evaluation mechanisms. They play crucial roles in managing and accessing data between actions, interceptors, and views (JSPs or other templates).

ValueStack and OGNL

ValueStack

The  ValueStack  in Struts2 is a stack-like structure that holds data objects during the request processing lifecycle. It manages the data from various sources such as actions, interceptors, and configuration files. The top object on the ValueStack represents the current context, allowing easy access to properties and methods within that context.

OGNL (Object-Graph Navigation Language)

OGNL  is a powerful expression language used in Struts2 for evaluating and manipulating objects on the ValueStack. It provides a concise syntax for navigating object graphs and accessing properties, making it easy to bind data between the action layer and the presentation layer (JSP pages). OGNL is also used in configuration files to specify dynamic values and conditions.

Using ValueStack and OGNL in Action Class

Consider an example where an action class sets a message attribute, which is then accessed in a JSP using OGNL through the ValueStack.

Example
java
package com.example.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {

    private String message = "Hello, World!";

    @Override
    public String execute() throws Exception {
        // Business logic
        return SUCCESS;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

hello-world.jsp
jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Struts2 Hello World</title>
</head>
<body>
    <h1>${message}</h1>
</body>
</html>

Explanation :

  • 1.  HelloWorldAction.java : The HelloWorldAction class defines a message attribute and provides getter and setter methods. In the execute() method, it returns SUCCESS, indicating that the action executed successfully.
  • 2.  hello-world.jsp : This JSP file accesses the message attribute using OGNL through the Struts2 tag library (<s:>). OGNL evaluates ${message} to retrieve the value from the ValueStack, which is populated with the message attribute from the action class.

Benefits of ValueStack and OGNL

  • Simplified Data Binding : OGNL provides a straightforward way to bind data between actions and views without requiring explicit data retrieval or manipulation code in JSPs.
  • Encapsulation : Actions can expose specific properties for display or manipulation in views without exposing their internal implementation details.
  • Dynamic Evaluation : OGNL allows dynamic evaluation of expressions, making it easy to access complex object graphs and perform conditional logic directly in JSPs or configuration files.

Conclusion

ValueStack and OGNL are fundamental components of Struts2, facilitating seamless communication and data transfer between actions and views. They simplify development by providing a powerful yet simple mechanism for handling data and expressions within the framework.