OGNL是Object-Graph Navigation Language(对象图导航语言)的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性。
下面在普通的project中,看一下ognl的各种表达式的运用。
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Dog {
private String name;
private String[] friends;
public String[] getFriends() {
return friends;
}
public void setFriends(String[] friends) {
this.friends = friends;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.suo.ognl;
import java.util.ArrayList;
public class OgnlTest {
public static void main(String[] args) throws OgnlException {
Person person=new Person();
person.setName("suo");
Dog dog=new Dog();
dog.setName("aHuang");
OgnlContext context=new OgnlContext();//Ognl的上下文环境
context.put("person", person);
context.put("dog", dog);
context.setRoot(person);//设置person为根元素
/**
* 若访问的是根元素中的属性,那么在解析的表达式中,只需要直接指定属性名就可以了。
*/
Object object1=Ognl.parseExpression("name");//此处写为getName()也可以
System.out.println(object1);
Object object2=Ognl.getValue(object1, context, context.getRoot());
System.out.println(object2);
/**
* 若访问的不是根元素的属性,那么在解析的表达式中,就要用“#”来指定访问哪个元素的属性。<br>
* 当然,访问根元素的属性,也可以用这种方式。
*/
Object object3=Ognl.parseExpression("#dog.name.toUpperCase()");//这里会调用get方法来获得属性
System.out.println(object3);
Object object4=Ognl.getValue(object3, context, context.getRoot());
System.out.println(object4);
Object object5=Ognl.parseExpression("#person.name");
System.out.println(object5);
Object object6=Ognl.getValue(object5, context, context.getRoot());
System.out.println(object6);
/**
* 调用静态方法的表达式为:@package.classname@methodname(parameter)
*/
Object object7=Ognl.parseExpression("@java.lang.Integer@toBinaryString(5)");
System.out.println(object7);
Object object8=Ognl.getValue(object7, context, context.getRoot());
System.out.println(object8);
/**
* Ognl默认的类是java.lang.Math,所以调用它中的静态方法或静态变量,无需指定类名
*/
Object object9=Ognl.parseExpression("@@min(4,8)");
System.out.println(object9);
Object object10=Ognl.getValue(object9, context, context.getRoot());
System.out.println(object10);
/**
* ognl还可以通过表达式来新建一个对象
*/
Object object11=Ognl.parseExpression("new java.util.LinkedList()");
System.out.println(object11);
Object object12=Ognl.getValue(object11, context, context.getRoot());
System.out.println(object12);
/**
* 还可以将上面的两个方法合二为一<br>
* ognl中,数组和集合是一样的,都是通过下标索引去访问的,构造集合使用“{}”的形式。
*/
Object object13=Ognl.getValue("{'aa','bb','cc','dd'}[3]", context, context.getRoot());
System.out.println(object13);
//数组
dog.setFriends(new String[]{"a","b","c"});
Object object14=Ognl.getValue("#dog.friends[1]", context, context.getRoot());
System.out.println(object14);
List<String> list=new ArrayList<String>();
list.add("suo");
list.add("love");
list.add("piao");
context.put("list", list);
//集合
Object object15=Ognl.getValue("#list[1]", context, context.getRoot());
System.out.println(object15);
//Map,此处的#号和以上的#号意义不同。
Object object16=Ognl.getValue("#{'k1':'v1','k2':'v2','k3':'v3'}['k2']", context, context.getRoot());
System.out.println(object16);
/**
* 对List的过滤,即将符合条件的元素过滤出来。
* 相当于取出表中的一行行符合条件的完整的记录,是原有集合的一个子集。
* 过滤的表达式的格式为:collection.{? expression}。
* #this表示当前正在迭代的对象。
*/
List<Person> persons=new ArrayList<Person>();
Person p1=new Person();
Person p2=new Person();
Person p3=new Person();
p1.setName("suo");
p2.setName("piao");
p3.setName("rachel");
persons.add(p1);
persons.add(p2);
persons.add(p3);
context.put("persons", persons);
//获得符合条件的所有元素:collection.{? expression}
System.out.println(Ognl.getValue("#persons.{? #this.name.length() > 3}", context, context.getRoot()));
//获得符合条件的第一个元素:collection.{^ expression}
System.out.println(Ognl.getValue("#persons.{^ #this.name.length() > 3}[0].name", context, context.getRoot()));
//获得符合条件的最后一个元素:collection.{$ expression}
System.out.println(Ognl.getValue("#persons.{$ #this.name.length() > 3}[0].name", context, context.getRoot()));
/**
* 投影,取出集合中对象的统一属性,所得的集合大小和原集合大小肯定是一样的。
* 格式为:collection.{expression}
* 这相当于是取表中的所有集合的相同的列。
*/
//取得集合中的所有对象的name属性
System.out.println(Ognl.getValue("#persons.{name}",context,context.getRoot()));
//若集合中的对象的name值长度小于等于4,那么将其替换成World
System.out.println(Ognl.getValue("#persons.{#this.name.length()<=4 ? 'World' : #this.name}",context,context.getRoot()));
}
}