日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

浅谈OGNL表达式

發布時間:2024/6/21 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 浅谈OGNL表达式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

OGNL(Object-Graph Navigation Language):對象視圖導航語言

${user.addr.name}這樣的寫法就叫對象視圖導航

OGNL不僅可以視圖導航,支持EL表達式更加豐富的功能

EL表達式取值要從11個內置對象中取:requestScopesessionScopeapplicationScopepageScopepageContextparams、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

總結

以上是生活随笔為你收集整理的浅谈OGNL表达式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。