美文网首页
OGNL语法

OGNL语法

作者: 蕊er | 来源:发表于2017-11-06 14:09 被阅读0次

    OGNL

    Object-Graph Navigation Language ---- 对象视图导航语言

    语法

    User user = new User("李雷", 15);
    User user1 = new User("韩梅梅", 15);
    
    Map<String, User> userMap = new HashMap<>();
    userMap.put("user1", new User("杰克", 15));
    userMap.put("user1", new User("肉丝", 15));
    
    OgnlContext ognlContext = new OgnlContext();
    
    • 存入Root

      ognlContext.setRoot(user);
      ognlContext.setRoot(user1);
      
    • 存入OgnlContext

      ognlContext.setValues(userMap);
      
    • 取值

      //由Root取值 - - - - 属性名为name的值
      String name = (String) Ognl.getValue("name", ognlContext, ognlContext.getRoot());
      System.out.println("name:" + name);
      //由OgnlContext取值 - - - - #代表OgnlContext 即OgnlContext中key为user1的值的name属性
      String name1 = (String) Ognl.getValue("#user1.name", ognlContext,     ognlContext.getRoot());
      System.out.println("name:" + name1);
      
      /*name:韩梅梅
        name:肉丝*/
      
    • 赋值

      //由Root赋值
      name = (String) Ognl.getValue("name='lilei'", ognlContext, ognlContext.getRoot());
      System.out.println("name:" + name);
      //由OgnlContext赋值
      name1 = (String) Ognl.getValue("#user1.name='jerry'", ognlContext, ognlContext.getRoot());
      System.out.println("name:" + name1);
      /*name:lilei
        name:jerry*/
      
    • 调用方法

      //由Root调用方法
      Ognl.getValue("setName('韩梅梅')", ognlContext, ognlContext.getRoot());
      name = (String) Ognl.getValue("getName()", ognlContext, ognlContext.getRoot());
      System.out.println("name:" + name);
      //由OgnlContext调用方法
      Ognl.getValue("#user1.setName('肉丝')", ognlContext, ognlContext.getRoot());
      name1 = (String) Ognl.getValue("#user1.getName()", ognlContext, ognlContext.getRoot());
      System.out.println("name:" + name1);
      /*name:韩梅梅
      name:肉丝*/
      
    • 调用静态方法 @的使用

      String s = (String) Ognl.getValue("@com.entity.Demo@same('你好')", ognlContext, ognlContext.getRoot());
      Double pi = (Double) Ognl.getValue("@@PI", ognlContext, ognlContext.getRoot());
      System.out.println("name:" + s);
      System.out.println("pi:" + pi);
      /*name:你好
      pi:3.141592653589793*/
      
    • Ognl创建对象

      • list

        List<String> list = (List) Ognl.getValue("{'one','two','three'}", ognlContext, ognlContext.getRoot());
        for (String ss : list) {
            System.out.printf(ss + " ");
        }
        System.out.println();
        String s1 = (String) Ognl.getValue("{'one','two','three'}[0]", ognlContext, ognlContext.getRoot());
        String s2 = (String) Ognl.getValue("{'one','two','three'}.get(2)", ognlContext, ognlContext.getRoot());
        Integer number = (Integer) Ognl.getValue("{'one','two','three'}.size()", ognlContext, ognlContext.getRoot());
        System.out.println(s1 + " " + s2 + " " + number);
        /*one two three
        one three 3*/
        
      • map

        s1 = (String) Ognl.getValue("#{'name':'one','age':'12'}['name']", ognlContext, ognlContext.getRoot());
         s2 = (String) Ognl.getValue("#{'name':'one','age':'12'}.get('name')", ognlContext, ognlContext.getRoot());
        number = (Integer) Ognl.getValue("#{'name':'one','age':'12'}.size()", ognlContext, ognlContext.getRoot());
        System.out.println(s1 + " " + s2 + " " + number);
        /**
        * one one 2
        */
        

    相关文章

      网友评论

          本文标题:OGNL语法

          本文链接:https://www.haomeiwen.com/subject/zcrlmxtx.html