• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
Online Java Training-Online Java Tutor-Private Java Classes

Online Java Training-Online Java Tutor-Private Java Classes

Personal Online Java Training through Skype

  • Home
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • Contact Us

OGNL in Struts 2

OGNL(Object-Graph Navigation language) is an expression language inherited by Struts from WebWork.

Use of OGNL

  • OGNL is used in struts to access model objects from a jsp page.
  • It can be used to bind GUI elements to model objects in struts framework.
  • It can be used to invoke methods of the model.
  • It can create lists,maps to be used with GUI.
  • Bind generic tags with model objects.

Value Stack

Value Stack is an object created prior to any struts action method is executed and is used to store actions and other objects. The struts framework stores the value stack in request attribute named “struts.valueStack” and is used by jsps to display action and other info.

Value Stack contains two logical units, the Object Stack and the Context Map.

valuestack in struts

Action and other objects are pushed into the Object Stack where as maps (parameters, request, session, application, attr) are stored in Context Map.

Maps in the Context Map

  1. parameters. Is a map containing the parameters in the current request.
  2. request. Is a map containing attributes in the current request.
  3. session. Is a map containing the session attributes for the current user.
  4. application. Is a map containing the ServletContext attributes.
  5. attr. Is a map that searches for attributes in the the order: request, session, application.

OGNL can be used to access  objects in the Object Stack and the Context Map. To access Context Map properties we use a # in front of the OGNL expression, if not used search will take place from Object Stack.

eg #session.code returns the value of the session attribute code.

Accessing Object Stack object properties

[0].message,[0][“message”],or [0][‘message’] returns the message property value of the object on top.

[1].duration,[1].[“duration”],or [1][‘duration’] returns the duration property of the second object.

To print the duration property of the first stack object we can use <s:property value=”[0].duration”/>

Searching of property in Object Stack

[1].message–>starts searching for the message property from the 1st object in stack if not found searches in the next object at index position[2] and goes on.

[0].message is same as writing only message. In both cases the searching starts from the 1st object in the Value Stack.

Reading Object Properties in the Context Map

To access properties of Objects in the Context Map we can use any of the following forms.

#object.propertyName

#object[‘propertyName’]

#object[“propertyName”]

The folowing expression returns the firstName property of an employee object stored as the request attribute.

#request[“employee”][“firstName”]

The following expression will search for the lastAccessedTime attribute in the request object. If it doesn’t find in the request will search in the session and consequently in the application object.

#attr[‘lastAccessedTime’]

Accessing Methods an Fields using OGNL

Methods and fields can be accessed through OGNL. e.g @[email protected] is used to access the static property DECEMBER in Calendar class. To call a static function we use @[email protected](); if we have a static function called now() in the Util class in pac1.pac2 package.

To call a nonstatic field or function we use. object.fieldname or we can use [0].datepattern where [0] refers to the first object in the value stack.

Filed Under: Struts

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Mr Chinmay

Chinmay Patel

Phone & Whatsapp +919853166385
[email protected]
Skype id: p.chinmay

Recent Posts

  • How to Learn Java in One day ? | JavaTutorOnline
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse
  • How to learn Java Programming Language the Best way ? | JavaTutorOnline
  • Recursion in Java Example Program | Understanding Java Recursion
  • Important Core Java Interview Questions and Answers
  • How to Create Threads in Java | Multithreading in Java | JavaTutorOnline
  • Why Multiple Inheritance in Java not supported?
  • A Simple Spring Mvc Hello World Example | Spring Mvc Tutorial
  • How to become a good Teacher |Ideal Teacher | Perfect Teacher
  • OGNL in Struts 2 Tutorial | JavaTutorOnline
Copyright © 2022 JavaTutorOnline