OGNL

1.OGNL的基本介绍

1. OGNL的全称是Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式语言
2.它是一种强大的表达式语言,让你通过简单一致的表达式语法来读取和设置Java对象的属性值,调用对象的方法,遍历整个对象的结构图,实现字段类型转换等功能。


2.案例

1.下面创建一个类
表达一下根对象和非根对象取值的特点和OGNL取值赋值。

package com.yinyi.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);//小芳
	}

}

然后看一下我们的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>动态方法调用</h3>
<a href="${pageContext.request.contextPath }/sy/demo_add.action">新增</a>
<a href="${pageContext.request.contextPath }/sy/demo_del.action">删除</a>

<h3>后台接收jsp传递参数的三种方式</h3>
<a href="${pageContext.request.contextPath }/sy/demo_accept1.action?num1=20&&num2=5">accept1</a>
<a href="${pageContext.request.contextPath }/sy/demo_accept2.action?cal2.num1=20&&cal2.num2=5">accept2</a>
<a href="${pageContext.request.contextPath }/sy/demo_accept3.action?sex=nv">accept3</a>

<h3>讲解OGNL</h3>
<a href="${pageContext.request.contextPath }/sy/stack_test1.action">ognl1</a>

</body>
</html>


创建一个类写我们得到方法

package com.yinyi.test;

import org.apache.struts2.ServletActionContext;

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

public class DemoAction {
	public String  test1() {
//		ValueStack是一个堆栈的容器 特点:先进后出
		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
		return "rs";
	}
	
	public String test2() { 
		// 栈:表示一个先进后出的数据结构
		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());
		return "rs";
	}
}

3.El表达式和OGNL的区别

1.El表达式方便在9大内置对象取值

2.OGNLContext通透来讲就是map集合,非根集合需要通过Key访问,根对象可以不用


4.总结

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

5、struts2中传递数据
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值