浅谈OGNL表达式

OGNL(Object-Graph Navigation Language):对象视图导航语言

${user.addr.name}这样的写法就叫对象视图导航

OGNL不仅可以视图导航,支持EL表达式更加丰富的功能

EL表达式取值要从11个内置对象中取:requestScopesessionScopeapplicationScopepageScope、  pageContextparams、paramValues、header、headerValues、cookie、initParams

OGNL表达式取值要从OGNLContext中取值,在Ognl上下文中有Root部分Context部分,Root部分放置任何对象作为root都可以,Context部分必须是Map类型

使用OGNL准备工作

  导包->struts2的包中已经包含了,所以不需要导入额外的jar包

  书写代码

@Test
    //准备工作
    public void fun1() throws Exception{
        //准备ONGL Context
            //准备ROOT
        User rootUser = new User("tom","18");
            //准备Context
        Map<String, User> context = new HashMap<String,User>();
        context.put("user1", new User("jack","18"));
        context.put("user2", new User("rose","22"));
        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);
        //书写OGNL 在双引号里写OGNL表达式即可
        Ognl.getValue("", oc, oc.getRoot());
    }

OGNL基本语法演示 取出root的值

    @Test
    //基本语法演示
    //取出oot中的属性值
    public void fun2() throws OgnlException{
        User rootUser = new User("tom","18");
        Map<String, User> context = new HashMap<String,User>();
        context.put("user1", new User("jack","18"));
        context.put("user2", new User("rose","22"));
        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);
        //书写OGNL
        //取出root中user对象的name属性
        String name = (String) Ognl.getValue("name", oc, oc.getRoot());
        String age = (String) Ognl.getValue("age", oc, oc.getRoot());
        System.out.println(name+" "+age);
    }
tom 18

OGNL取出Context的值

@Test
    //基本语法演示
    //取出context中的属性值
    public void fun3() throws OgnlException{
        User rootUser = new User("tom","18");
        Map<String, User> context = new HashMap<String,User>();
        context.put("user1", new User("jack","18"));
        context.put("user2", new User("rose","22"));
        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);
        //书写OGNL
        String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
        String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());
        String age = (String) Ognl.getValue("#user2.age", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(name2);
        System.out.println(age);
    }

jack
rose
22

OGNL赋值语法

@Test
    //赋值
    public void fun4() throws OgnlException{
        User rootUser = new User("tom","18");
        Map<String, User> context = new HashMap<String,User>();
        context.put("user1", new User("jack","18"));
        context.put("user2", new User("rose","22"));
        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);
        
        Ognl.getValue("name='jerry'", oc, oc.getRoot());
        String name = (String) Ognl.getValue("name", oc, oc.getRoot());
        //书写OGNL
        String name2 = (String) Ognl.getValue("#user1.name='zao',#user1.name", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(name2);
    }

jerry
zao

OGNL方法调用

    @Test
    //调用方法
    public void fun5() throws OgnlException{
        User rootUser = new User("tom","18");
        Map<String, User> context = new HashMap<String,User>();
        context.put("user1", new User("jack","18"));
        context.put("user2", new User("rose","22"));
        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);
        //书写OGNL
        Ognl.getValue("setName('lilei')", oc, oc.getRoot());
        String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());
        
        String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(name2);
    }

lilei
lucy

OGNL调用静态方法和静态常量

注意下面表达式
@全类名@方法名('传参')
@全类名@常量名
@@常量名

@Test
    public void fun6() throws OgnlException{
        User rootUser = new User("tom","18");
        Map<String, User> context = new HashMap<String,User>();
        context.put("user1", new User("jack","18"));
        context.put("user2", new User("rose","22"));
        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);
        //书写OGNL
        String name = (String) Ognl.getValue("@cn.itheima.a_ognl.HahaUtils@echo('hello shijie')", oc, oc.getRoot());
        Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
        Double pi1 = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(pi);
        System.out.println(pi1);
    }

hello shijie
3.141592653589793
3.141592653589793

OGNL创建 list map集合

@Test
    public void fun7() throws OgnlException{
        User rootUser = new User("tom","18");
        Map<String, User> context = new HashMap<String,User>();
        context.put("user1", new User("jack","18"));
        context.put("user2", new User("rose","22"));
        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);
        //书写OGNL
        //创建list对象
        Integer size = (Integer) Ognl.getValue("{'tom','jerr','jack'}.size()", oc, oc.getRoot());
        String name = (String) Ognl.getValue("{'tom','jerr','jack'}[0]", oc, oc.getRoot());
        String name2 = (String) Ognl.getValue("{'tom','jerr','jack'}.get(1)", oc, oc.getRoot());
        System.out.println(size);
        System.out.println(name);
        System.out.println(name2);
        //创建map对象
        Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());
        String name3 = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());
        Integer age = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot());
        System.out.println(size2);
        System.out.println(name3);
        System.out.println(age);
    }

3
tom
jerr
2
tom
18

原文地址:https://www.cnblogs.com/moster/p/7944647.html