OGNL使用例子

  1. 导入相关的jar包

    		<dependency>
    			<groupId>ognl</groupId>
    			<artifactId>ognl</artifactId>
    			<version>3.3.4</version>
    		</dependency>
    
    
  2. 使用例子代码

    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);
        }
    
    
    
    
    }
    
    

posted on 2023-02-23 21:38  zhangyukun  阅读(78)  评论(0编辑  收藏  举报

导航