OGNL in Struts2 Tutorial

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

时间: 2024-10-12 21:24:42

OGNL in Struts2 Tutorial的相关文章

JAVAEE学习——struts2_03:OGNL表达式、OGNL与Struts2的结合和练习:客户列表

一.OGNL表达式 1.简介 OGNL:对象视图导航语言.  ${user.addr.name} 这种写法就叫对象视图导航. OGNL不仅仅可以视图导航.支持比EL表达式更加丰富的功能. 2.使用OGNL准备工作 2.1导包 struts2 的包中已经包含了.所以不需要导入额外的jar包 2.2代码准备 @Test //准备工作 public void fun1() throws Exception{ //准备OGNLContext //准备Root User rootUser = new Us

框架 day29 Struts2-上传,下载,OGNL与Struts2的结合(值栈与ActionContext),Struts2标签

文件上传 1.1回顾 *浏览器端: <formmethod="post" enctyp="multipart/form-data"> <inputtype="file" name="image"> <inputtype="submit"> *服务器端: 手动方式 第三方:commons-fileupload  核心类ServletFileUpload 1.2介绍 *str

ValueStack和OGNL达到Struts2形式的数据存储原理

(1)最近学习struts相框,我们在快乐struts强大.为了便于使用转发,但不了解详细的内部数据存储: (2)网上找了很多关于struts数据存储的原理,但我还没有找到一个具体的解释,本书上找到了 ValueStack和OGNL实现Struts2表单数据的存储原理 特分享给大家! (3)下面是部分截图:解说了关于struts框架中数据表单等的存储! watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGxnZW4xNTczODc=/font/5a6L5

ValueStack和OGNL实现Struts2表单数据的存储原理

(1)最近学习了struts框架,我们愉悦于struts的强大,方便转发器的使用,但是却不了解内部数据的具体存储: (2)在网上找了很多关于struts数据存储的原理,但是一直没有找到一个详细解说的,偶尔在一本书上找到了 ValueStack和OGNL实现Struts2表单数据的存储原理 特分享给大家! (3)以下是部分截图:讲解了关于struts框架中数据表单等的存储! 感觉这本书讲解的确实很好!需要的回复油箱地址发给你们!

OGNL表达式struts2标签“%,#,$”

一.什么是OGNL,有什么特点? OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言.是一种可以方便地操作对象属性的开源表达式语言.OGNL有如下特点: 1.支持对象方法调用,形式如:objName.methodName(): 2.支持类静态的方法调用和值访问,表达式的格式为@[类全名(包括包路)]@[方法名 |  值名],例如: @java.lang.String@format('foo %s', 'bar')或@tutorial.

OGNL与Struts2标签展示页面

1.概述 2.OGNL 注:ctrl+shift+f(调整格式)

OGNL与Struts2标签展示页面(二)

1.概述 2.数据标签

OGNL与Struts2标签展示页面(三)

1.概述  2.UI标签

struts2值栈存取数据小结(使用ognl表达式)

前言:使用ognl和struts2标签库的时候要在 jsp页面 中 加入这么一行 <%@ taglib uri="/struts-tags" prefix="s" %> struts(统一格式): <action name="ognl" class="com.demo.test1.OnglAction" method="ognl1"> <result name="og