VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/struts-2-ognl/

⇱ Struts 2 OGNL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Struts 2 OGNL

Last Updated : 28 Apr, 2025

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.

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.

Step 3: Configuration Files

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

Step 4: Create web.xml file

To test JstlView, we are generating a JSP file.

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.

Comment

Explore