Struts2复习之OGNL(1)

来源:互联网 发布:java磁力解析源码 编辑:程序博客网 时间:2024/05/16 05:37

1. 当使用OGNL调用静态方法的时候,需要按照如下语法编写表达式:  @package.classname@methodname(parameter)
2. 对于 OGNL 来说,java.lang.Math 是其的默认类,如果调用java.lang.Math的静态方法时,无需指定类的名字,比如: @@min(4, 10);
3. 对于OGNL来说,数组与集合是一样的,都是通过下标索引来去访问的。构造集合的时候使用{ … }形式。
4. 使用OGNL来处理映射(Map)的语法格式如下所示: #{‘key1’: ‘value1’ , ‘key2’: ‘value2’ , ‘key3’: ‘value3’};
5. 过滤(filtering) :collection.{? expression}
6.  OGNL 针对集合提供了一些伪属性(如 size,isEmpty) ,让我们可以通过属性的方式来调用方法(本质原因在于集合当中的很多方法并不符合JavaBean的命则),       但我么你依然还可以通过调用方法来实现与伪属性相同的目的。
7. 过滤(filtering),获取到集合中的第一个元素:collection.{^ expression}
8. 过滤(filtering),获取到集合中的最后一个元素:collection.{& expression}
9. 在使用过滤操作时,我们通常都会使用#this,该表达式用于代表当前正在迭代的集合中的对象(联想增强的for循环) 
10.    投影(projection):collection.{expression}
11.    过滤与投影之间的差别:类比于数据库中的表,过滤是取行的操作,而投影是取列的操作。

demo:

import java.util.ArrayList;import java.util.List;import ognl.Ognl;import ognl.OgnlContext;public class OgnlTest{public static void main(String[] args) throws Exception{Person person = new Person();person.setName("zhangsan");Dog dog2 = new Dog();dog2.setName("hello");person.setDog(dog2);Dog dog = new Dog();dog.setName("wangcai");OgnlContext context = new OgnlContext();context.put("person", person);context.put("dog", dog);context.setRoot(person); //根对象Object object = Ognl.parseExpression("#person.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, 10)");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("----------------------------");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<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("#{'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}  ['key3']", context, context.getRoot()));System.out.println("----------------------------");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);//过滤(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()));}}


 

1 0
原创粉丝点击