码迷,mamicode.com
首页 > 其他好文 > 详细

OGNL in Struts2 Tutorial

时间:2015-06-20 15:38:57      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

 

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.

技术分享

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 @java.util.Calendar@DECEMBER is used to access the static property DECEMBER in Calendar class. To call a static function we use @pac1.pac2.Util@now(); 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.

OGNL in Struts2 Tutorial

标签:

原文地址:http://www.cnblogs.com/hephec/p/4590601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!