struts(2)OGNL

1. OGNL

1.1 OGNL的全称是Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式语言
1.2 OgnlContext(ongl上下文)其实就是Map (教室、老师、学生)

	map 教室
  OgnlContext=根对象(1)+非根对象(N)
	老师:跟对象	1
	学生:非根对象	n
	
  非根对象要通过"#key"访问,根对象可以省略"#key"

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

1.3 把根对象和非根对象说出来

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

1、ActionContext一次请求创建一次
2、值栈取值从上往下,取到为止,如果已经拿到,不再往下找。
2. ValueStack

2.1 值栈
先进后出的数据结构,弹夹 push/pop
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 *action
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");
4. struts2中传递数据

可以使用作用域,但更多的是利用ValueStack或ActionContext

核心点:

1、OGNL的全称是Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式语言
2、OgnlContext(ongl上下文)其实就是Map (教室、老师、学生)
3、根对象和非根对象的理解
4、ValueStack
同一请求中只创建一个上下文
先压action,再呀modeldriven
从上往下找值

5、struts2中传递数据

代码测试
导入以下类
在这里插入图片描述
Demo7.java

ServletActionContext.getContext().getValueStack();

Student.java

package com.bk201.two.test;

public class Student {
	private String name;

	private String number;

	public Student() {
		super();
	}

	public Student(String name, String number) {
		super();
		this.name = name;
		this.number = number;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", number=" + number + "]";
	}

}

Employee .java

package com.bk201.two.test;

public class Employee {
	private String name;

	private Address address;

	private Integer salary;

	public Employee() {
		super();
	}

	public Employee(String name, Integer salary) {
		super();
		this.name = name;
		this.salary = salary;
	}

	public Integer getSalary() {
		return salary;
	}

	public void setSalary(Integer salary) {
		this.salary = salary;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Employee [name=" + name + ", address=" + address + ", salary=" + salary + "]";
	}

}

Address.java

package com.bk201.two.test;

public class Address {
	private String city;

	private String country;

	public Address() {
		super();
	}

	public Address(String city, String country) {
		super();
		this.city = city;
		this.country = country;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	@Override
	public String toString() {
		return "Address [city=" + city + ", country=" + country + "]";
	}

}

Manager .java

package com.bk201.two.test;

public class Manager {
	private String name;

	public Manager() {
		super();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Manager [name=" + name + "]";
	}

}

工具类OgnlException.java

package com.bk201.two.test;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

/**
 * 用于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);
		}
	}
}

Demo1.java

package com.bk201.two.test;

import ognl.OgnlContext;
import ognl.OgnlException;

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对象是根对象(请注意根对象和非根对象表达式的区别)
		String employeeName = (String) OnglExpression.getValue("name", ctx, e);
		System.out.println(employeeName);

		// 表达式#manager.name将执行m.getName(),注意:如果访问的不是根对象那么必须在前面加上一个名称空间,例如:#manager.name
		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);
	}

}

运行结果:
在这里插入图片描述
Struts_sy.xml:

<action name="stack_*" class="com.bk201.two.test.DemoAction" method="{1}">
	<result name="rs">/rs.jsp</result>
	</action>

Demo7.java

package com.bk201.two.test;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;

public class Demo7 {
	/**
	 * 
	 * 值栈的使用
	 * 
	 */
	public static void main1(String[] args) {
		// 栈:表示一个先进后出的数据结构
		ValueStack vs = ServletActionContext.getContext().getValueStack();
		// push方法把项压入栈顶
		vs.push(new Employee("zs", 22));
		vs.push(new Employee("ls", 22));
		vs.push(new Employee("ww", 22));

		// 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 main(String[] args) {
		ValueStack vs = ServletActionContext.getContext().getValueStack();
		vs.push(new Employee("张雇员", 2000));// 1
		vs.push(new Student("小明同学", "s001"));// 0
		System.out.println(vs.findValue("name"));//小明同学
		System.out.println(vs.findValue("salary2"));//2000

		ActionContext ac = ActionContext.getContext();
	}
}

demo1.jsp

<%@ 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>

	 <h3>调用OGNL</h3>
	 <a href="${pageContext.request.contextPath }/sy/stack_test1.action">ognl</a>
	<a href="${pageContext.request.contextPath }/sy/stack_test2.action">ognl2</a>
</body>
</html>

结果
test1:
在这里插入图片描述
test2:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值