欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OGNL(Struts2)

程序员文章站 2022-04-18 11:47:08
...

OGNL(重在理解)

1.1:ognl全称为Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式

1.2:OgnlContext=根对象(1)+非根对象(N)
非根对象要通过"#key"访问,根对象可以省略"#key"

  注1:context:英文原意上下文,环境/容器  

重点
① 一个上下文中只能有一个根对象
②跟对象与非根对象取值的区别
a:根对象取值,只需要直接通过跟对象的属性即可
b:非根对象的取值必须通过指定的上下文容器中的#key.属性去取

案列1:
package com.zking.test;
import ognl.OgnlContext;
import ognl.OgnlException;

public class Demo1 {

	/**
	 * @param args
	 * @throws OgnlException
	 * 重点:
	 * 		 ①:一个上下文中只有一个根元素
	 *       ②:取根对象的值只需要直接通过根对象的属性即可
	 *       ③:取非根对象的值必须通过指定上下文(容器)的#key
	 */
	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上下文(容器)中根元素(employee员工)的name属性    小李
		String employeeName = (String) OnglExpression.getValue("name", ctx, e);
		System.out.println(employeeName);

		// 表达式#manager.name将执行m.getName(),注意:如果访问的不是根对象那么必须在前面加上一个名称空间,例如:#manager.name
		//取出ognl上下文(容器)中的非根元素name值(manager)必须通过上下文参数的#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);

		/** ********************** 赋值操作 *************************** */
		//往ognl上下文的根对象的name属性赋值  小明
		OnglExpression.setValue("name", ctx, e, "小明");
		employeeName = (String) OnglExpression.getValue("name", ctx, e);
		System.out.println(employeeName);

		//往ognl上下文的非根对象的name属性赋值  孙经理
		OnglExpression.setValue("#manager.name", ctx, e, "孙经理");
		managerName = (String) OnglExpression.getValue("#manager.name", ctx, e);
		System.out.println(managerName);

		//往ognl上下文的根对象的name属性赋值  小芳
		OnglExpression.setValue("#employee.name", ctx, e, "小芳");
		employeeName = (String) OnglExpression.getValue("name", ctx, e);
		System.out.println(employeeName);
	}

}

ValueStack

2.1值栈
重点
①ActionContext 请求一次就创建一次。
②值栈取值从上往下取,取到为止,如果已经拿到就不在往下找。

案例2:
package com.zking.two.web;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import com.zking.test.Employee;
import com.zking.test.Student;

public class Demo7 {
	/**
	 * 
	 * 值栈的使用
	 * ActionContext上下文一次请求创建一次
	 * 值栈取值上从上往下取,取到为止,如果拿不到 不在往下找。
	 * 
	 * 
	 */
	public static void main1(String[] args) {
		// 栈:表示一个先进后出的数据结构
		ActionContext ac=ActionContext.getContext();
		System.out.println(ac);
		ValueStack vs=ac.getValueStack();//已经初始化好了 里面已近有值了
		// push方法把项压入栈顶
		vs.push(new Employee("zs", 22));
		vs.push(new Employee("ls", 22));
		vs.push(new Employee("ww", 22));
		输出顺序为:ww ls  zs

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

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

		//ActionContext ac = ActionContext.getContext();
	}
	
	public String execute() {
		main1(null);
		System.out.println("-------------");
		main2(null);
		return "success";
	}
}

2.2 为什么要使用ValueStack作为根对象
放到值栈中的对象都可视为根对象

从小到大
page -> request -> session -> application

从上至下
A
B
C
D

//伪代码
*.action
3. ActionContext
3.1 ActionContext ac = ActionContext.getContext();//保证同一请求中只创建一个上下文
request
session
application
parameters
ValueStack(root)//根对象

3.2 向ValueStack压栈
push(XxxAction)//helloAction
push(ModelDirver.getModel())//model不为null user

3.3 Map<String,String[]> map = request.getParamterMap(); 压action代码详解
//参数名==OGNL表达式
{“userName”:“aaa”,“uname”:“bbb”,“upwd”:“ccc”,“age”:“22”}

  setValue("userName", ac, vs, "aaa");
  setValue("uname", ac, vs, "bbb");
  setValue("upwd", ac, vs, "ccc");
  setValue("age", ac, vs, "22");

Struts赋值方式

①:提供get和set方法
②:实现ModelDriven接口
③:通过导航对象的方式
④:req.getParament();要有作用域
作用域从下到大排序为
page–>request–>session–>application