OGNL的使用方式

OGNL(Object Graph Navigation language)


OgnlContext:上下文对象,存在一个唯一的叫做根的对象(root),

可以通过程序设定上下文当中的那个对象作为根对象。


在OGNL中,如果表达式没有使用#号,那么OGNL会从根对象中寻找该属性对应的get方法,如果寻找的不是根对象中

的属性,那么则需要以#号开头告诉OGNL,去寻找你所指定的特定对象中的属性。


当使用OGNL调用静态的方法的时候,需要按照如下的语法编写表达式:

@package.classname@methodname(parameters)


对于OGNL来说,java.lang.Math是其默认的类,如果调用java.lang.Math

的静态方法时,无需指定类名。@@min(5,6)


对于OGNL来说:数组与集合一样的,都是通过下表索引来去访问的

构造集合的时候通过{.....}形式来访问。



使用OGNL来处理映射(Map)的语法格式如下:

#{'key1','value1','key2','value2','key3','value3'};

通过key来访问。

eg:System.out.println(Ognl.getValue("#{'one':'value1','two':'value2','three':'value3'}['one']", context,context.getRoot());


过滤(filtering):

collection.{?expression}基本语法格式。


OGNL针对集合提供了一些伪属性(如size,isEmpty),让我们

可以通过属性的方式来调用方法(本质原因在于集合当中的很多的方法并不符合

JavaBean的命名规则),但还可以通过调用方法来实现与伪属性相同的目的。


过滤(filtering),获取集合中的第一个元素collection.{^ expression}

过滤(filtering),获取集合中的最后一个元素collection.{$ expression}

在使用过滤操作时,我们通常都会使用#this,该表达式代表当前正在迭代的

集合中的对象(类比:增强的for循环)。for (Person p:persons){}


投影(projection):基本语法:collection.{expression}

eg:

类比数据库中的投影,过滤是对行的操作,投影式对列的操作。

数据库:select  name from emp where age > 50;

其中select name 就相当于投影,而where age > 50就相当于过滤。



------------------------------------------------------------------------------------------------------------------------------------------

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import bean.Dog;
import bean.Person;


public class OgnlTest {
	public static void main(String[] args) throws Exception {
		Dog innerDog = new Dog ();
		innerDog.setName("lisi");
		Person person = new Person ();
		person.setName("zhangsan");
		person.setDog(innerDog);
		Dog dog = new Dog ();
		dog.setName("wangcai");
		OgnlContext context = new OgnlContext ();
		context.put("person", person);
		context.put("dog", dog);
		//将person对象设置成根对象
		context.setRoot(person);
		Object object = Ognl.parseExpression("dog.name");
		System.out.println(object);
		Object object2 = Ognl.getValue(object, context,context.getRoot());
		System.out.println(object2);
		System.out.println("--------------------------");
		Object object3 = Ognl.parseExpression("#person.name");
		System.out.println(object3);
		Object object4 = Ognl.getValue(object3, context,context.getRoot());
		System.out.println(object4);
		System.out.println("----------------------------");
		Object object5 = Ognl.parseExpression("#dog.name");
		System.out.println(object5);
		Object object6 = Ognl.getValue(object5, context,context.getRoot());
		System.out.println(object6);
		System.out.println("----------------------------------");
		//调用方法
		Object object7 = Ognl.parseExpression("name.toUpperCase().length()");
		System.out.println(object7);
		Object object8 = Ognl.getValue(object7, context,context.getRoot());
		System.out.println(object8);
		System.out.println("----------------------------------");
		//调用静态方法
		Object object9 = Ognl.parseExpression("@java.lang.Integer@toBinaryString(10)");
		System.out.println(object9);
		Object object10 = Ognl.getValue(object9, context,context.getRoot());
		System.out.println(object10);
		System.out.println("----------------------------------");
		Object object11 = Ognl.parseExpression("@@min(4,6)");
		System.out.println(object11);
		Object object12 = Ognl.getValue(object11, context,context.getRoot());
		System.out.println(object12);
		System.out.println("----------------------------------");
		//生成对象
		Object object13 = Ognl.parseExpression("new java.util.LinkedList()");
		System.out.println(object13);
		Object object14 = Ognl.getValue(object13, context,context.getRoot());
		System.out.println(object14);
		System.out.println("----------------------------------");
		//生成list对象
		Object object15 = Ognl.getValue("{'aa','bb','cc'}[0]", context,context.getRoot());
		System.out.println(object15);//在OGNL中,数组和列表看成是一样的。
		System.out.println(object15 instanceof ArrayList);
		System.out.println("----------------------------------");
		//访问数组中的元素
		dog.setFriends(new String[]{"aa","bb","cc"});
		Object object16 = Ognl.getValue("#dog.friends[1]", context,context.getRoot());
		System.out.println(object16);
		System.out.println("----------------------------------");
		List<String> list = new ArrayList<String> ();
		list.add("hello");
		list.add("world");
		list.add("hello world");
		context.put("list", list);
		System.out.println(Ognl.getValue("#list[0]", context,context.getRoot()));
		System.out.println("------------------------------------");
		System.out.println(Ognl.getValue("#{'one':'value1','two':'value2','three':'value3'}['one']", context,context.getRoot()));
		System.out.println("---------------------------------------------------------------------");
		//System.out.println(Ognl.getValue("#list.{? this.length() > 6}", context,context.getRoot()));
		List<Person> persons = new ArrayList<Person> ();
		Person p1 = new Person ();
		Person p2 = new Person ();
		Person p3 = new Person ();
		p1.setName("zhangsan");
		p2.setName("lisi");
		p3.setName("wangwu");
		persons.add(p1);
		persons.add(p2);
		persons.add(p3);
		context.put("persons", persons);
		//notices:此处的集合值得是List和Array
		//过滤(filter),collection.{? expression}   eg:#this表示当前待操作的元素
		System.out.println(Ognl.getValue("#persons.{? #this.name.length () > 4}.isEmpty()", context,context.getRoot()));
		//过滤,获取到集合中的第一个元素,collection.{^ expression}
		List list2 = (List)Ognl.getValue("#persons.{^ #this.name.length () > 4}", context,context.getRoot());//返回的仍然是集合,只不过集合中只有一个元素
		System.out.println(list2);
		//过滤 获得最后一个元素
		System.out.println(Ognl.getValue("#persons.{$ #this.name.length () > 4}[0].name", context,context.getRoot()));
		System.out.println("-----------------------------------------------------------");
		//投影(projection),collection. {expression}
		System.out.println (Ognl.getValue("#persons.{#this.name.length()<=5?'hello':#this.name}", context,context.getRoot()));
		System.out.println("------------------------------------------------------------");//result:[zhangsan, hello, wangwu]
		System.out.println(Ognl.getValue("#persons.{'hello'}", context,context.getRoot()));//result:[hello, hello, hello]
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值