OGNL介绍:
1 OGNL的全称是Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式语言
2 OgnlContext(ongl上下文)其实就是Map
OgnlContext=根对象(1)+非根对象(N)
非根对象要通过"#key"访问,根对象可以省略"#key"
所谓的对象图导航语言,在EL表达式中可表示为:
class Book
private String bid;
private String bname;
private Category c
class Category
private String cid;
private String name;
案列说明:
四个实体类:
student(学生类):
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 + "]";
}
}
Manager :
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 + "]";
}
}
Employee :
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 + "]";
}
}
Manager :
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 + "]";
}
}
然后第一个案例:
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("1:"+employeeName);//小李
// 表达式#manager.name将执行m.getName(),注意:如果访问的不是根对象那么必须在前面加上一个名称空间,例如:#manager.name
String managerName = (String) OnglExpression.getValue("#manager.name",
ctx, e);
System.out.println("2:"+managerName);//张经理
// 当然根对象也可以使用#employee.name表达式进行访问
employeeName = (String) OnglExpression.getValue("#employee.name", ctx,
e);
System.out.println("3:"+employeeName);//小李
/** ********************** 赋值操作 *************************** */
OnglExpression.setValue("name", ctx, e, "小明");
employeeName = (String) OnglExpression.getValue("name", ctx, e);
System.out.println("4:"+employeeName);//小明
OnglExpression.setValue("#manager.name", ctx, e, "孙经理");
managerName = (String) OnglExpression.getValue("#manager.name", ctx, e);
System.out.println("5:"+managerName);//孙经理
OnglExpression.setValue("#employee.name", ctx, e, "小芳");
employeeName = (String) OnglExpression.getValue("name", ctx, e);
System.out.println("6:"+employeeName);//小芳
}
}
上面的代码出来的效果就是:
案例二:
public class Demo2 {
public String test1() {
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
ActionContext ac = ActionContext.getContext();
return "rs";
}
}
前台向后台传值
<a href="${pageContext.request.contextPath }/sy/stack_test1.action">ognl1</a>
然后我们来解释为什么cal1.num1有值,而num1却取不到值?
都自动struts后台取值方法有3种,一种实现接口,一种通过get和set方法取,还有一种通过对象点属性名,我已经提供了get方法,其实应该是能取到的,但是为什么就是到用的时候取不到值呢?
其实这就和ValueStack的压栈有关系了,压栈的结构是先进后出,而ValueStack的取值是取到了值就会返回,不会往下取了。
而ValueStack对取值的压栈是先把xxxAction也就是我们通过set和get方法取值的方式先压入栈底,然后再把ModelDriver压入,所有会先取到通过ModelDriver来获取值的,
而获取到了就返回了,导致后面的值不会获取了