Struts2之OGNL表达式与值栈

1、概述:OGNL的全称是Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式语言,Struts框架使用OGNL作为默认的表达式语言。

2、OGNL 有一个上下文(Context)概念,它是一个map结构,因为它实现了java.utils.Map 的接口。OgnlContext(ognl上下文)=根对象(1个)+非根对象(n个),非根对象要通过"#key"访问,根对象可以省略"#key"。可通过以下代码进行理解:

OnglExpression工具类:

/**
 * 用于OGNL表达计算的一个工具类
 */
public class OnglExpression {
	private OnglExpression() {
		
	}

	/**
	 * 根据OGNL表达式进行取值操作
	 * @param expression:ognl表达式
	 * @param ctx:ognl上下文
	 * @param rootObject:ognl根对象
	 * @return
	 */
	public static Object getValue(String expression, OgnlContext ctx, Object rootObject) {
		try {
			return Ognl.getValue(expression, ctx, rootObject);
		} catch (OgnlException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 根据OGNL表达式进行赋值操作
	 * @param expression:ognl表达式
	 * @param ctx:ognl上下文
	 * @param rootObject:ognl根对象
	 * @param value:值对象
	 */
	public static void setValue(String expression, OgnlContext ctx, Object rootObject, Object value) {
		try {
			Ognl.setValue(expression, ctx, rootObject, value);
		} catch (OgnlException e) {
			throw new RuntimeException(e);
		}
	}
}

测试类:

public class Demo1 {

	/**
	 * @param args
	 * @throws OgnlException
	 */
	public static void main(String[] args)  {
		Employee e = new Employee();
		e.setName("小李");

		Manager m = new Manager();
		m.setName("张经理");

		// 创建OGNL上下文,而OGNL上下文实际上就是一个Map对象
		OgnlContext ctx = new OgnlContext();

		// 将员工和经理放到OGNL上下文当中去
		ctx.put("employee", e);
		ctx.put("manager", m);
		ctx.setRoot(e);// 设置OGNL上下文的根对象

		/** ********************** 取值操作 *************************** */
		// 表达式name将执行e.getName(),因为e对象是根对象(请注意根对象和非根对象表达式的区别)
		//通过ognl表达式在上下文中获取根节点的name属性值
		String employeeName = (String) OnglExpression.getValue("name", ctx, e);//小李
		System.out.println(employeeName);

		// 表达式#manager.name将执行m.getName(),注意:如果访问的不是根对象那么必须在前面加上一个名称空间,例如:#manager.name
		//通过ognl表达式在上下文中获取非根对象的属性值(#非根对象在容器中的key.属性)
		String managerName = (String) OnglExpression.getValue("#manager.name", ctx, e);//张经理
		System.out.println(managerName);

		// 当然根对象也可以使用#employee.name表达式进行访问
		employeeName = (String) OnglExpression.getValue("#employee.name", ctx, e);//小李
		System.out.println(employeeName);

		/** ********************** 赋值操作 *************************** */
		OnglExpression.setValue("name", ctx, e, "小明");
		employeeName = (String) OnglExpression.getValue("name", ctx, e);//小明
		System.out.println(employeeName);

		OnglExpression.setValue("#manager.name", ctx, e, "孙经理");
		managerName = (String) OnglExpression.getValue("#manager.name", ctx, e);//孙经理
		System.out.println(managerName);

		OnglExpression.setValue("#employee.name", ctx, e, "小芳");
		employeeName = (String) OnglExpression.getValue("name", ctx, e);//小芳
		System.out.println(employeeName);
	}
}

打印结果:

总结:1、一个上下文中只有一个根对象
            2、取根对象的值,只需要直接通过根对象属性即可
            3、非根对象取值必须通过指定的上下文容器中的#key.属性去取

 3、ognl是一种思想,而Struts沿用了这种思想,所以Struts中也有上下文,即actionContext,actionContext一次请求只创建一次。ValueStack(值栈)是actionContext的根对象,值栈是先进后出的数据结构,放到值栈中的对象都可视为根对象。

public class OgnlAction {
	
	public String execute() {
		main1(null);
		System.out.println("--------------");
		main2(null);
		return "success";
	}
	
	/**
	 * 值栈的使用
	 */
	public static void main1(String[] args) {
		// 栈:表示一个先进后出的数据结构
		ActionContext ac = ActionContext.getContext();
		ValueStack vs = ac.getValueStack();
		// push方法把项压入栈顶
		vs.push(new Employee("zs", 22));//Employee实体类的两个属性为name和salary
		vs.push(new Employee("ls", 22));
		vs.push(new Employee("ww", 22));

		// pop方法移除栈顶对象并作为此函数的值返回该对象
		Employee e = (Employee) vs.pop();
		System.out.println(e.getName());//ww
		e = (Employee) vs.pop();
		System.out.println(e.getName());//ls
		e = (Employee) vs.pop();
		System.out.println(e.getName());//zs
	}

	/**
	 * 此例用于模拟struts2的值栈计算过程
	 * @param args
	 */
	public static void main2(String[] args) {
		ActionContext ac = ActionContext.getContext();
		ValueStack vs = ac.getValueStack();
		vs.push(new Employee("张雇员", 2000));//Employee实体类的两个属性为name和salary
		vs.push(new Student("小明同学", "s001"));//Student实体类的两个属性为name和uid
		System.out.println(vs.findValue("name"));
		System.out.println(vs.findValue("salary"));
	}
}

打印结果:

值栈中值的顺序图如下: 

扫描二维码关注公众号,回复: 4107887 查看本文章

总结:1、值栈取值从上往下,取到为止,如果已经拿到,不再往下找。
             2、valuestack又相当于一个容器,容器里面存放的对象,包含的所有属性,都可以算valuestack的属性

 4、Struts2中给参数赋值:a、在Action中定义属性,并提供get/set方法

                                                b、实现ModelDriven接口,返回实体,不能为null,不需要提供get/set方法

当ModelDriven返回实体的属性和Action中属性重名时,ModelDriven中优先级更高,有如下代码:

CaseAction类代码:

public class CaseAction implements ModelDriven<User> {

	private User user = new User();//User实体类的属性为uname和uid
	private String uname;

	@Override
	public User getModel() {
		return user;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String execute() {
		System.out.println("uname:"+uname);
		System.out.println(user);
		return "success";
	}
}

HTML代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>jsp与子控制器相互传值的问题</h1>
	<a href="${pageContext.request.contextPath }/sy/caseAction.action?uname=xx">测试</a>
</body>
</html>

打印结果:

结果显示action里定义的uname属性为null,而实体类里的uname属性获取到了传过来的值,这是因为在向ValueStack压栈时,是先压action,再压modeldriven的,而值栈取值时又是从上往下取的。

值栈中值的顺序图如下:

猜你喜欢

转载自blog.csdn.net/weixin_42687829/article/details/83011144