1. 导入相关的jar包
<dependency>
			<groupId>ognl</groupId>
			<artifactId>ognl</artifactId>
			<version>3.3.4</version>
		</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  1. 使用例子代码
package com.lomi.ognl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.lomi.entity.Goods;
import com.lomi.entity.out.GoodsOut;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.junit.Test;

import java.util.Arrays;
import java.util.Map;

/**
 *
 *  ognl 对象图导航语言,可以通过表达式从Java对象,json ,map中获取到值
 *  如果表达式取到不存在的属性值会报错
 *
 * @author ZHANGYUKUN
 */
public class OGNLTest {


    public static String  staticValue = "静态属性";
    public static String getStaticValue() {
        return staticValue;
    }

    private String name = "张三丰";
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 使用 # 是区_value里面的值,没有#取的root里面的值
     *
     * @throws OgnlException
     */
    @Test
    public void t1() throws OgnlException {
        //this就是root节点
        OgnlContext context = (OgnlContext) Ognl.createDefaultContext(this);
        context.put("goods",Goods.randomGoods());

        Object ognlName1 = Ognl.parseExpression("#goods.name");
        Object ognlName2 = Ognl.parseExpression("name");

        Object name1 = Ognl.getValue( ognlName1, context, context.getRoot());
        Object name2 = Ognl.getValue( ognlName2, context, context.getRoot());

        System.out.println("获取到的name1:"  + name1);
        System.out.println("获取到的name2:"  + name2);

    }

    /**
     *   context 里面的值的时候,可以任意传入 context ,context只是一个map 而已,不一定非要用  OGNLContext的 _value 属性
     * @throws OgnlException
     */
    @Test
    public void t2() throws OgnlException {
        Map<String, Object> objectMap = BeanUtil.beanToMap(Goods.randomGoods());

        OgnlContext context = (OgnlContext) Ognl.createDefaultContext( objectMap );


        //并且这个 context 不一定需要是 OgnlContext里面的 _value
        Object ognl = Ognl.parseExpression("#id");
        Object value2 = Ognl.getValue(ognl, objectMap  ,context.getRoot());
        System.out.println(value2);
    }


    /**
     *  如果是Java对象 取不存在的属性会报错
     * @throws OgnlException
     */
    @Test
    public void t3() throws OgnlException {
        Map<String, Object> objectMap = BeanUtil.beanToMap(Goods.randomGoods());

        OgnlContext context = (OgnlContext) Ognl.createDefaultContext( objectMap );


        //并且这个 context 不一定需要是 OgnlContext里面的 _value
        Object ognl = Ognl.parseExpression("#id.name");
        Object value2 = Ognl.getValue(ognl, objectMap  ,context.getRoot());
        System.out.println(value2);
    }


    /**
     *  map取不到不会报错,但是继续取下级就会报错
     * @throws OgnlException
     */
    @Test
    public void t4() throws OgnlException {
        Map<String, Object> objectMap = BeanUtil.beanToMap(Goods.randomGoods());

        OgnlContext context = (OgnlContext) Ognl.createDefaultContext( objectMap );


        //并且这个 context 不一定需要是 OgnlContext里面的 _value
        Object ognl = Ognl.parseExpression("#name2");
        Object value2 = Ognl.getValue(ognl, objectMap  ,context.getRoot());
        System.out.println(value2);
    }



    /**
     * 获取数组的值
     * @throws OgnlException
     */
    @Test
    public void t5() throws OgnlException {
        Map<String, Object> objectMap = BeanUtil.beanToMap(Goods.randomGoods());
        objectMap.put("list", Arrays.asList(  Goods.randomGoods(),Goods.randomGoods(),Goods.randomGoods() ));

        System.out.println(JSONUtil.formatJsonStr( JSONUtil.toJsonStr(  objectMap ) ));

        OgnlContext context = (OgnlContext) Ognl.createDefaultContext( objectMap );


        //并且这个 context 不一定需要是 OgnlContext里面的 _value
        Object ognl = Ognl.parseExpression("#list[0].name");
        Object value1 = Ognl.getValue(ognl, objectMap  ,context.getRoot());
        System.out.println(value1);


        //投影语法,取到把 集合里面的某个属性取出来构成一个集合
        Object ognl2 = Ognl.parseExpression("#list.{name}");
        Object value2 = Ognl.getValue(ognl2, objectMap  ,context.getRoot());
        System.out.println(value2);


        //筛选
        Object ognl3 = Ognl.parseExpression("#list.{? stock>2000}");
        Object value3 = Ognl.getValue(ognl3, objectMap  ,context.getRoot());
        System.out.println(value3);


        //筛选以后取第一个
        Object ognl4 = Ognl.parseExpression("#list.{^ stock>2000}");
        Object value4 = Ognl.getValue(ognl4, objectMap  ,context.getRoot());
        System.out.println(value4);

        //筛选以后取最后一个
        Object ognl5 = Ognl.parseExpression("#list.{$ stock>2000}");
        Object value5 = Ognl.getValue(ognl5, objectMap  ,context.getRoot());
        System.out.println(value5);

    }


    /**获取静态值
     * @throws OgnlException
     */
    @Test
    public void t6() throws OgnlException {
        //this就是root节点
        OgnlContext context = (OgnlContext) Ognl.createDefaultContext(this);

        Object ognlName1 = Ognl.parseExpression("@com.lomi.ognl.OGNLTest@staticValue");
        Object name1 = Ognl.getValue( ognlName1, context, context.getRoot());

        System.out.println("获取到的name1:"  + name1);
    }

    /**调用方法
     * @throws OgnlException
     */
    @Test
    public void t7() throws OgnlException {
        //this就是root节点
        OgnlContext context = (OgnlContext) Ognl.createDefaultContext(this);

        //调用普通方法
        Object ognlName1 = Ognl.parseExpression("getName()");
        Object name1 = Ognl.getValue( ognlName1, context, context.getRoot());

        //调用有参数的方法
        Object ognlName2 = Ognl.parseExpression("setName( '张三' )");
        Object name2 = Ognl.getValue( ognlName2, context, context.getRoot());


        Object name3 = Ognl.getValue( ognlName1, context, context.getRoot());

        //静态方法
        Object name4 = Ognl.getValue( "@com.lomi.ognl.OGNLTest@getStaticValue()", context, context.getRoot());

        System.out.println("获取到的name1:"  + name1);
        System.out.println("获取到的name2:"  + name2);
        System.out.println("获取到的name3:"  + name3);
        System.out.println("获取到的name4:"  + name4);
    }


    /**
     * 逗号表达式,每个都执行,获取的值是最后一个
     *
     * @throws OgnlException
     */
    @Test
    public void t8() throws OgnlException {
        //this就是root节点
        OgnlContext context = (OgnlContext) Ognl.createDefaultContext(this);

        //调用普通方法
        Object ognlName1 = Ognl.parseExpression("getName(),@com.lomi.ognl.OGNLTest@getStaticValue()");
        Object ognlName2 = Ognl.parseExpression("@com.lomi.ognl.OGNLTest@getStaticValue(),getName()");
        Object name1 = Ognl.getValue( ognlName1, context, context.getRoot());
        Object name2 = Ognl.getValue( ognlName2, context, context.getRoot());

        System.out.println("获取到的name1:"  + name1);
        System.out.println("获取到的name2:"  + name2);
    }




}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.