ognl

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值