正井猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

                                                         OGNL

一.概述

以下内容摘自Ognl的官网:

  OGNL stands for Object-Graph Navigation Language; it is an expression language for getting and setting properties of Java objects, plus other extras such as list projection and selection and lambda expressions. You use the same expression for both getting and setting the value of a property.   

  The Ognl class contains convenience methods for evaluating OGNL expressions. You can do this in two stages, parsing an expression into an internal form and then using that internal form to either set or get the value of a property; or you can do it in a single stage, and get or set a property using the String form of the expression directly.

    OGNL的全称叫做对象图导航语言,是一个用于获取与设置Java对象的表达式语言,还附加一些例如集合投影、过滤、Lambda表达式的功能。你可以使用同一个表达式实现属性的赋值或取值。

    Ognl的类中包含的很方便的方法实现OGNL表达式的赋值。实现这个功能你需要两步,解析一个表达式使之称为一种内部的形式然后再用这种内部的形式对属性赋值或取值;或者你可以实现这个功能只用一步,直接用字符串来实现属性的取值或者赋值。

二. 代码案例

2.1 定义两个JavaBean

public class Dog {
    private String name;
    //setter and getter
}
public class Person {
    private String name;
    private Dog dog;
    //setter and getter
}  

2.2 测试用例

2.2.1 获取属性

@Test
public void test1(){
    Dog dog = new Dog();
    dog.setName("wangcai");
        
    Dog dog1 = new Dog();
    dog1.setName("wangwang");
        
    Person person = new Person();
    person.setName("zhagnsan");
    person.setDog(dog1);
        
    OgnlContext context = new OgnlContext(); //实例化一个Ognl的上下文
    context.put("person", person); 
    context.put("dog", dog);
        
    context.setRoot(person);  //将person设置为ongl的根
    try {
        //使用Ognl.paraseExpression(str)来解析ognl表达式。dog是person这个根对象的属性,可以直接写属性名
        Object obj = Ognl.getValue(Ognl.parseExpression("dog.name"), context, context.getRoot());
        System.out.println(obj);                           
        System.out.println("=================================");

        //#person.name获取根对象person的name属性, #person代表的person这个对象,如要拿到某个对象要使用#
        obj = Ognl.getValue(Ognl.parseExpression("#person.name"), context, context.getRoot());
        System.out.println(obj);     
        System.out.println("=================================");
            
        //#dog表示的是context.put("dog", dog)这个对象
        obj = Ognl.getValue("#dog.name", context, context.getRoot());
        System.out.println(obj);
        System.out.println("==================================");
            
        //此时name表示的是person这个根对象的属性,直接写上属性名即可
        obj = Ognl.getValue("name", context, context.getRoot());
        System.out.println(obj);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

输出结果:

wangwang

================================================

zhagnsan

================================================

wangcai

================================================

zhagnsan

2.2.2 方法调用

@Test
public void test2(){
    try{
        Person person = new Person();
        person.setName("zhagnsan");
            
        OgnlContext context = new OgnlContext();
        context.put("person", person);

        context.setRoot(person);
            
        //可以直接调用方法,name是根对象person的属性
        Object obj = Ognl.getValue("name.toUpperCase().length()", context, context.getRoot());
        System.out.println(obj);
        System.out.println("=================================");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

输出结果: 8

2.2.3 调用静态方法

@Test
public void test3(){
    OgnlContext context = new OgnlContext();
    try {
        //调用静态方法的语法: @类的全名@静态方法名
        Object obj = Ognl.getValue("@java.lang.Integer@toBinaryString(10)", context, context.getRoot());
        System.out.println(obj);
    } catch (OgnlException e) {
        e.printStackTrace();
    }
}

输出结果:1010

2.2.4 java.lang.Math的处理

@Test
public void test4(){
    OgnlContext context = new OgnlContext();
    try {
        //java.lang.Math为ognl的内置对象,@@即可表示对java.lang.Math的引用
        Object obj = Ognl.parseExpression("@@min(10, 4)");
        obj = Ognl.getValue(obj, context, context.getRoot());
        System.out.println(obj);
    } catch (OgnlException e) {
        e.printStackTrace();
    }
}

输出结果:4

2.2.5 List集合的处理

@Test
public void test5(){
    OgnlContext context = new OgnlContext();
    try {
        Object obj = Ognl.getValue("{'aa', 'bb', 'cc'}", context, ontext.getRoot());
        System.out.println(obj instanceof List);
    } catch (OgnlException e) {
        e.printStackTrace();
    }
}

输出结果:true

@Test
public void test7(){
    OgnlContext context = new OgnlContext();
    try {
        List<String> list = new ArrayList<String>();
        list.add("hello");
        list.add("world");
        list.add("welcome");
        context.put("list", list);
            
        Object obj = Ognl.getValue("#list[1]", context, context. getRoot());
        System.out.println(obj);
    } catch (OgnlException e) {
        e.printStackTrace();
    }
}

输出结果:world

2.2.6 Map集合

@Test
public void testMap(){
    OgnlContext context = new OgnlContext();
    try {
        /*
        Map<String, String> map = new HashMap<String, String>();
        map.put("aa", "abc");
        map.put("bb", "zz");
        map.put("cc", "gg");
            
        context.put("map", map);
            
        Object obj = Ognl.getValue("#map['aa']", context, context.getRoot());
        */
        Object obj = Ognl.getValue("#{'aa':'zz', 'bb':'cc', 'dd':'xx'}['aa']", context, context.getRoot());
        System.out.println(obj);
    } catch (OgnlException e) {
        e.printStackTrace();
    }
}

 输出结果:zz

2.2.7 过滤

//过滤,语法格式Collection.{? expr},返回的是一个集合
@Test
public void testFilter(){
    OgnlContext context = new OgnlContext();
    try {
        List<String> list = new ArrayList<String>();
        list.add("hello");
        list.add("world");
        list.add("welcome");
        list.add("helloworld");
        context.put("list", list);
            
        /*
        Object obj = Ognl.getValue("#list.{? #this.length() gt 5}", context, context.getRoot());
        System.out.println(obj);
        */
        //Object obj = Ognl.getValue("#list.{? #this.length() gt 5}.size()", context, context.getRoot()); //size是伪属性,isEmpty也是伪属性
        Object obj = Ognl.getValue("#list.{? #this.length() gt 5}.size", context, context.getRoot()); 
        System.out.println(obj);  
        System.out.println("==============================");
        /**
         * collectoin.{^ expr} 获取到过滤后的集合中的第一个元素
         */
        obj = Ognl.getValue("#list.{^ #this.length() > 5}", context, context.getRoot()); 
        System.out.println(obj);
        System.out.println("==============================");
            
        /**
         * collectoin.{$ expr} 获取到过滤后的集合中的最后一个元素
         */
        obj = Ognl.getValue("#list.{$ #this.length() > 5}[0]", context, context.getRoot()); 
        System.out.println(obj);
    } catch (OgnlException e) {
        e.printStackTrace();
    }
}

 输出结果:

2

==============================

[welcome]

==============================

helloworld

2.2.8 投影

/**
 * 投影 collection.{expr}, 返回的依然是集合,并且长度不会变化,仅仅只是属性的个数发生的变化。拿数据库来类比:过滤相当于取行的操作(行数的可能会变化),投影相当于数据库的取列的操作。
 */
@Test
public void testProjectoin(){
    OgnlContext context = new OgnlContext();
    try {
        List<Person> list = new ArrayList<Person>();
        Person p1 = new Person();
        p1.setName("zhangsan");
            
        Person p2 = new Person();
        p2.setName("lisi");
            
        Person p3 = new Person();
        p3.setName("wangwu");
        list.add(p1);
        list.add(p2);
        list.add(p3);
        context.put("list", list);
            
        /*//将List中每个Person的name取出来,放到一个集合中
        //Object obj = Ognl.getValue("#list.{name}", context, context.getRoot()); 
        Object obj = Ognl.getValue("#list.{#this.name}", context, context.getRoot()); //与上一行代码实现相同的功能
        System.out.println(obj);
        */
        //当名字长度大于5,就用helloworld来替换,否则保持不变
        //Object obj = Ognl.getValue("#list.{#this.name.length() > 5 ? 'helloworld' : #this.name}", context, context.getRoot()); 
        Object obj = Ognl.getValue("#list.{name.length() > 5 ? 'helloworld' : name}", context, context.getRoot()); //与上一行代码实现相同的功能 
        System.out.println(obj);
    } catch (OgnlException e) {
        e.printStackTrace();
    }
}

 输出结果:

[helloworld, lisi, helloworld]

 

下一篇:Struts2对Ognl的支持

posted on 2016-06-19 16:32  正井猫  阅读(1745)  评论(0编辑  收藏  举报