Struts2 Ognl

downpour在iteye上的struts2专栏中对struts2 ognl的描述
http://struts2.group.iteye.com/group/wiki/1353-ognl-catalyst-for-data-operation-in-struts2

http://struts2.group.iteye.com/group/wiki/1356-how-to-use-ognl-in-struts2

http://struts2.group.iteye.com/group/wiki/1365-passing-parameters-in-struts2


OGNL概述
OGNL(Object-Graph Navigation language)对象图导航语言,它是一种功能强大的表达式语言(Expression Language,简称为EL),可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。


它本身和struts2没有一点关系,被集成在struts2中并作了相应的扩展用来帮助实现数据转移和类型转换。webwork2和struts2中使用OGNL取代原来的EL来做界面数据绑定。



我们主要关注它提供的静态的setValue和getValue方法,通过传递三个主要的参数来实现OGNL的一切操作,这三个参数分别为:

1、表达式:指定做什么,通过Ognl.parseExpression()指定

2、根对象:指定对谁做,针对根对象(Root Object)的存取操作的表达式是不需要增加#符号进行区分的。

3、上下文环境:在OGNL的内部,所有的操作都会在一个特定的上下文环境中运行,是一个Map结构,称之为OGNLContext,实现了java.utils.Map的接口。当你在调用OGNL的取值或者设值的方法时,可能会自己定义一个Context,并且将它传递给方法。事实上,你所传递进去的这个Context,会在OGNL内部被转化成OGNLContext,而你传递进去的所有的键值对,也会被OGNLContext接管维护,这里有点类似一个装饰器,向你屏蔽了一些其内部的实现机理。


有了表达式和根对象,我们实际上已经可以使用OGNL的基本功能。例如,根据表达式对根对象进行取值或者设值工作。



示例:

导入ognl-x.x.x.jar和它关联的javassist-x.x.x.jar

public class OgnlTest {

	@Test
	public void testOnglSetAndGetValue() throws Exception{
		Dept dept = new Dept();
		dept.setId(1);
		dept.setDeptName("研发部门");
		
		Person person = new Person();
		person.setName("张三");
		person.setDept(dept);
		
		//no Context
		Object object = Ognl.getValue(Ognl.parseExpression("name"), person);
		assertEquals("张三", object);
		Object object2 = Ognl.getValue(
                        Ognl.parseExpression("dept.deptName"), person);
		assertEquals("研发部门", object2);
		Ognl.setValue(Ognl.parseExpression("dept.deptName"), 
                        person, "营销部门");
		assertEquals("营销部门", person.getDept().getDeptName());
		
		//with Context
		OgnlContext context = new OgnlContext();
		context.put("person", person);
		context.put("dept", dept);
		context.put("struts2", "struts2 ognl");
		
		Object object3 = Ognl.getValue(Ognl.parseExpression("name"), context, person);
		assertEquals("张三", object3);
		Object object4 = Ognl.getValue(
                          Ognl.parseExpression("dept.deptName"), context, person);
		assertEquals("营销部门", object4);
		
		Object strutsognl = Ognl.getValue(Ognl.parseExpression("struts2"), context);
		assertEquals("struts2 ognl", strutsognl);
		Object strutsognl2 = Ognl.getValue(
                                    Ognl.parseExpression("#struts2"), context, person);
		assertEquals("struts2 ognl", strutsognl2);
		Ognl.setValue(Ognl.parseExpression("struts2"), context, "ognl");
		Object strutsognl3 = Ognl.getValue(
                         Ognl.parseExpression("#struts2"), context, person);
		assertEquals("ognl", strutsognl3);
	}
}

struts2中的OGNL

The framework sets the OGNL context to be our ActionContext, and the value stack to be the OGNL root object. (The value stack is a set of several objects, but to OGNL it appears to be a single object.) Along with the value stack, the framework places other objects in the ActionContext, including Maps representing the application, session, and request contexts. These objects coexist in the ActionContext, alongside the value stack (our OGNL root).


Struts2 Ognl_第1张图片



The Action instance is always pushed onto the value stack. Because the Action is on the stack, and the stack is the OGNL root, references to Action properties can omit the#marker. But, to access other objects in the ActionContext, we must use the#notation so OGNL knows not to look in the root object, but for some other object in the ActionContext.


Struts2 Ognl_第2张图片

你可能感兴趣的:(Struts2 Ognl)