OGNL简介
OGNL介绍
- OGNL的全称是Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式语言
- OGNL相当于一个上下文(Context)概念,说白了上下文就是一个MAP结构,它实现了java.utils.Map 的接口。Struts框架默认就支持Ognl表达式语言。(从struts项目必须引入ognl.jar包可以看出)、
OgnlContext=根对象(1)+非根对象(N)
根对象只能有一个,而非根对象可以有多个, 非根对象要通过"#key"访问,根对象可以省略"#key"直接通过根对象属性就可以
封装两个实体类进行测试
员工类
学生类
创建demo我们调用测试根对象取值和非根对象取值的现象区别,以及OGNL取值和赋值的案例
package com.chen.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);
}
}
从上往下的顺序
OGNL向ValueStack压栈
从前台传值到后台
struts-sy.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sy" extends="base" namespace="/sy">
<action name="/demo_*" class="com.chen.web.HelloAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="/stack_*" class="com.chen.test.DemoAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
</package>
</struts>
前台jsp
<a href="${pageContext.request.contextPath}/sy/stack_test1.action?num1=20&&num2=5">OGNL</a>
DemoAction
package com.chen.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("salary"));
ActionContext ac = ActionContext.getContext();
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";
}
}
我们从结果上可以看到它是相当于堆栈一个特点,先进后出,可以使用ValueStack作为根对象
放到值栈中的对象都可视为根对象
test1结果:
test2结果: