ONGL
简介
什么是ongl
OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,OGNL是一个开源项目,读者可以访问其官方站点www.ognl.org以获得源代码和相关资料。OGNL是一种功能强大的EL(Expression Language,表达式语言),可以通过简单的表达式来访问Java对象中的属性。
OGNL先在WebWork项目中得到应用,也是Struts 2框架视图默认的表达式语言,可以说,OGNL表达式是Struts 2框架的特点之一。
为什么使用ongl
为什么使用OGNL
相对于其它的表达式语言,OGNL的功能更为强大,它提供了很多高级而必需的特性,例如强大的类型转换功能、静态或实例方法的执行、跨集合投影,以及动态lambda表达式定义等。
OgnlContext(ongl上下文)其实就是Map集合(一个上下文中只有一个根对象), OgnlContext=根对象(1)+非根对象(N),
非根对象要通过"#key"访问,根对象可以省略"#key"。
根对象和非根对象的理解(区别):
1.一个上下文中只有一个根对象
2.取根对象的值,只需要直接通过根对象属性即可
3.非根对象取值必须通过指定的上下文容器中的#key.属性去取。
注意:
ActionContext一次请求创建一次
值栈取值从上往下,取到为止,如果已经拿到,不再往下找。
ValueStack
先进后出的数据结构(堆栈),弹夹 push/pop
使用ValueStack作为根对象,是因为放到值栈中的对象都可视为根对象
案例
取值
package com.yz.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);
}
}
结果
package com.yz.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"));//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());//ww
e = (Employee) vs.pop();
System.out.println(e.getName());//ls
e = (Employee) vs.pop();
System.out.println(e.getName());//zs
return "rs";
}
}
OGNL向ValueStack压栈
public class HelloAction implements ModelDriven<Cal>{
private HttpServletRequest request;
private Cal cal1=new Cal();
private Cal cal2;
private String sex;
private String num1;
public String getNum1() {
return num1;
}
public void setNum1(String num1) {
this.num1 = num1;
}
public Cal getCal2() {
return cal2;
}
public void setCal2(Cal cal2) {
this.cal2 = cal2;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String accept1() {
System.out.println("cal1:"+cal1);
System.out.println("num1:"+num1);
return "rs";
}
}
结果