OGNL in Struts2 Tutorial
2015-06-20 15:17
567 查看
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
parameters. Is a map containing the parameters in the current request.
request. Is a map containing attributes in the current request.
session. Is a map containing the session attributes for the current user.
application. Is a map containing the ServletContext attributes.
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.
相关文章推荐
- DBUtil
- SpringMVC傻瓜式教程搭建
- eclipse仿VS2013皮肤
- Eclipse - Method NewStringUTF() could not be resolved
- java I/O 输入输出流详解
- Eclipse--theme--color 插件设置
- java 线程synchronized 线程同步
- Music Player With Desktop LRC Scolling - Java
- java中的静态变量、静态方法与静态代码块
- Java串口通信详细解释
- java产生任意范围内不重复的随机数
- Spring中的AOP简述
- Spring整合JMS(四)——事务管理
- Spring整合JMS(三)——MessageConverter介绍
- Spring整合JMS(二)——三种消息监听器
- Spring整合JMS(一)——基于ActiveMQ实现
- springmvc使用@ResponseBody返回json乱码解决方法
- 1. Java基础
- springMVC拦截器
- JAVASE学习笔记:第十二章 集合