Open In App

Struts 2 OGNL

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

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.






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.




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






<!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.




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

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

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.


Article Tags :