Open In App

Struts 2 OGNL

Last Updated : 25 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Struts 2 OGNL determines that the data on the ValueStack may be referenced and altered using the powerful expression language known as the Object-Graph Navigation Language (OGNL). Additionally, OGNL facilitates type conversion and data transmission. The JSP Expression Language and the OGNL are quite similar. The foundation of OGNL is the notion of a root or default object in the context. The pound sign, or markup notation, can be used to access the attributes of the default or root object.

Syntax to access the action property using OGNL:

<s:property value="propertyName"/>

Struts 2 OGNL Methods

  • CompoundRoot obtainRoot(): Obtain the CompoundRoot, which is where the items that were stacked are kept.
  • Object pop(): Take the item at the top of the stack and take it down.
  • Object findValue(String expr): Assess the provided expression against the stack using the standard search order to determine a value.
  • void set(String key, Object o): Places an object with the specified key on the stack so that findValue(key,…) may get it.

Step-by-Step Implementation of Struts 2 OGNL

Step 1: Create Action

Examining the subsequent action class, we are gaining access to valueStack and subsequently assigning a few keys that we will retrieve using OGNL in our view, which is the JSP page.

Java




import java.util.ArrayList;
import java.util.List;
  
public class Test {
    //data members
    private List<String> myList = 
                 new ArrayList<String>();
   
    //business logic
    public String execute(){
        myList.add("Bhatia");
        myList.add("Rik");
        myList.add("Sahdev");
        myList.add("Rajesheshri");
        myList.add("Himanshu");
        myList.add("Viyu");
        myList.add("Shweta");
        myList.add("Bhart");
        return "success";    
    }
   
    public List<String> getMyList() {
        return myList;
    }
   
    public void setMyList(List<String> myList) {
        this.myList = myList;
    }        
}


Step 2: Create Views

In the WebContent folder of your Eclipse project, create the HelloWorld.jsp JavaScript file below. If the action is successful, this view will be shown.

XML




<%@ 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>


Step 3: Configuration Files

This XML file specifies an interceptor called jsonValidatorWorkflowStack and an additional result called input.

XML




<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   
<struts>
   
    <package name="user" extends="struts-default">
        <action name="welcome" 
                   class="org.geeksforgeeks.action.Test">
            <result name="success">/welcome.jsp</result>
        </action>
    </package>
   
</struts>


Step 4: Create web.xml file

To test JstlView, we are generating a JSP file.

XML




<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
 <head>
    <title>Struts 2 OGNL</title>
 </head>
 <body>
    <h3>This is a OGNL</h3>
   
    <strong><em> All list items: <s:property value="myList" /> </em></strong>
    <strong><em> Second list item: <s:property value="myList[1]" /> </em></strong>
    <strong><em> List size: <s:property value="myList.size" /> </em></strong>
   
 </body>
</html>


Output:

OGNL in Struts2 Output

After clicking on “Click here”, we will see the list values by using OGNL:

OGNL-in-struts-2

Conclusion

So, this is Struts 2 OGNL. The JSP Expression Language and the OGNL are quite similar. The foundation of OGNL is the notion of a root or default object in the context. The pound sign, or markup notation, can be used to access the attributes of the default or root object.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads