ognl

来源:互联网 发布:编程需要学的软件 编辑:程序博客网 时间:2024/04/28 05:03
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

public class OgnlTest {
    public static void main(String[] args) {
        Persion persion = new Persion();
        Dog dog = new Dog();
        persion.setName("ZhuGouLiang");
        dog.setName("WangCai");
        OgnlContext context = new OgnlContext();
        context.put("persion", persion);
        context.put("dog", dog);
        context.setRoot(persion);

        getOgnlValue(context, "name");
        getOgnlValue(context, "#persion.name");
        getOgnlValue(context, "#dog.name");
        getOgnlValue(context, "name.toUpperCase()");
        getOgnlValue(context, "#persion.name");
        // 调用静态方法
        getOgnlValue(context, "@java.lang.Integer@toBinaryString(10)");
        // Ognl的默认class是Math
        getOgnlValue(context, "@@min(10,5)");
        // Ognl访问静态的属性
        getOgnlValue(context, "@@PI");

        getOgnlValue(context, "new java.util.ArrayList()");
        // 数组和集合对象是一样的,都是通过索引下标得到value
        getOgnlValue(context, "{'aa','bb','cc','dd'}");
        getOgnlValue(context, "{'aa','bb','cc','dd'}[2]");

        dog.setFriends(new String[] { "aa", "bb", "cc", "dd" });
        getOgnlValue(context, "#dog.friends");
        getOgnlValue(context, "#dog.friends[0]");
        // 集合伪属性 ---size isEmpty
        // 集合--list
        List<String> list = new ArrayList<String>();
        list.add("chenyanshan");
        list.add("liuyoujuan");
        list.add("chuanzhiboke");
        context.put("list", list);
        getOgnlValue(context, "#list[0]");
        // 集合--map
        Map<String, String> map = new HashMap<String, String>();
        map.put("me", "chenyanshan");
        map.put("she", "liuyoujuan");
        map.put("other", ".....");
        context.put("map", map);
        getOgnlValue(context, "#map['me']");

        getOgnlValue(context,
                "#{'me':'chenyanshan','she':'liuyoujuan','other':'.....'}['me']");

        // filter---collection.{? expression}
        List<Persion> persions = new ArrayList<Persion>();
        Persion ps1 = new Persion();
        Persion ps2 = new Persion();
        Persion ps3 = new Persion();
        ps1.setName("chenyanshan");
        ps2.setName("liuyoujuan");
        ps3.setName("other");
        persions.add(ps1);
        persions.add(ps2);
        persions.add(ps3);
        context.put("persions", persions);
        getOgnlValue(context, "#persions.{? #this.name.length() > 6}.size()");

        // filter 获取集合的第一个元素---collection.{^ expression}
        getOgnlValue(context, "#persions.{^ #this.name.length() > 6}[0].name");

        // filter 获取集合的最后一个元素---collection.{^ expression}
        getOgnlValue(context, "#persions.{$ #this.name.length() > 6}[0].name");

        // 投影(projection)---collection.{expression}    
        getOgnlValue(context, "#persions.{name}");
        getOgnlValue(context, "#persions.{#this.name.length()<=6 ? 'hello world' : #this.name}");
        
    }

    private static void getOgnlValue(OgnlContext context, String valueFromString) {
        Object obj_class;
        try {
            obj_class = Ognl.parseExpression(valueFromString);
            System.out.println(obj_class);
            Object obj_value = Ognl.getValue(obj_class, context,
                    context.getRoot());
            System.out.println(obj_value);
            System.out.println("-------------");

        } catch (OgnlException e) {
            e.printStackTrace();
        }
    }
}



public class Dog {
    private String name;

    private String[] friends;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String[] getFriends() {
        return friends;
    }

    public void setFriends(String[] friends) {
        this.friends = friends;
    }
}


public class Persion {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


0 0
原创粉丝点击