Struts2复习之OGNL(1)(二)
Object object15 = Ognl.getValue("{'aa', 'bb', 'cc', 'dd'}[2]", context, context.getRoot());
System.out.println(object15);
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
list = new ArrayList
(); 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("#{'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'} ['key3']", context, context.getRoot())); System.out.println("----------------------------"); List
persons = new ArrayList
(); 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); //过滤(filtering),collection.{ expression} System.out.println(Ognl.getValue("#persons.{ #this.name.length() >
4}[0].name", context, context.getRoot())); //过滤(filtering),获取到集合中的第一个元素, collection.{^ expression} System.out.println(Ognl.getValue("#persons.{^ #this.name.length() > 4}[0].name", context, context.getRoot())); //过滤(filtering),获取到集合中的第一个元素, collection.{$ expression} 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.{name}", context, context.getRoot())); System.out.println("----------------------------"); System.out.println(Ognl.getValue("#persons.{#this.name.length() <= 5 'Hello World' : #this.name}", context, context.getRoot())); } }