栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

OGNL简介

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OGNL简介

今日目标:

了解struts的传值的优先级

OGNL介绍:

1,OGNL的全称是Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式语言
2,OGNL相当于一个上下文(OgnlContext)概念,就是一个Map结构(教室  老师  学生)

它实现了java.utils.Map 的接口。Struts框架默认就支持ognl表达式语言。(从struts项目必须引入ognl.jar包可以看出)

比如:Map 教室

OgnlContext=根对象(1)+非根对象(N)

        老师:跟对象    1
        学生:非根对象    n

根对象只能有一个,而非根对象可以有多个, 非根对象要通过"#key"访问,根对象可以省略"#key"直接通过根对象属性就可以封装两个实体类进行测试

一,测试

(创建Demo1,我们调用测试根对象取值和非根对象取值的现象区别,以及OGNL取值和赋值的案例)

工具类

package com.lgs.test;

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


public class Onglexpression {
	private Onglexpression() {
	}

	
	public static Object getValue(String expression, OgnlContext ctx,
			Object rootObject) {
		try {
			return Ognl.getValue(expression, ctx, rootObject);
		} catch (OgnlException e) {
			throw new RuntimeException(e);
		}
	}

	
	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);
		}
	}
}

员工类

package com.lgs.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 + "]";
	}

}

经理类

package com.lgs.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 + "]";
	}

}

测试类  Demo1

package com.lgs.test;

import ognl.OgnlContext;
import ognl.OgnlException;

public class Demo1 {

	
	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);
	}

}

效果展示:(从上往下的顺序)

二,值栈的使用

值栈的特点:1,值栈是符合先进后出的特点的        2,取值是从上至下取到为止        3,赋值,谁在上面就先赋给谁

2.1,Demo7

package com.lgs.test;

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

public class Demo7 extends ActionSupport{
	
	
	public String ognl1() {
		// 栈:表示一个先进后出的数据结构
		ValueStack vs = ActionContext.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());
		return "bookEdit";
	}

	
	public String ognl2() {
		ValueStack vs = ActionContext.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("salary"));
		return "bookEdit";
	}
}

2.2,xml配置

2.3,前端jsp

 测试结果:先进后出

 取值测试结果:从上至下取到为止

赋值: 有俩个相同的的属性uname,从上往下开始,谁在上就先赋给谁

 OK! 到这就结束了,希望能帮到你!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322119.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号