java學習筆記-- struts2框架:OGNL表達式

OGNL取值範圍分兩部分,root、Context兩部分

可以放置任何對象作爲ROOT,CONTEXT中必須是Map鍵值對

示例:

準備工作:

複製代碼
    public void fun1() throws Exception {
        // 準備ONGLContext
        // 準備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();
        // 將rootUser作爲root部分
        oc.setRoot(rootUser);
        // 將context這個Map作爲Context部分
        oc.setValues(context);
        // 書寫OGNL
        Ognl.getValue("", oc, oc.getRoot());
        //在""中書寫OGNL表達式即可
    }
複製代碼

User類:

 View Code

 

語法:

從root中取出對象:

        // 取出root中user對象的name屬性
        String name = (String) Ognl.getValue("name", oc, oc.getRoot());
        Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());

 

從Context中取出對象:

        // 取出context中鍵爲user1對象的name屬性
        String name1 = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
        String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());
        Integer age = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());

 

爲屬性賦值:

        // 將root中的user對象的name屬性賦值
        Ognl.getValue("name='jerry'", oc, oc.getRoot());

給context賦值:

        String name2 = (String) Ognl.getValue("#user1.name='張三'", oc, oc.getRoot());

 

調用對象的方法:

複製代碼
        // 調用root中user對象的setName方法
        Ognl.getValue("setName('張三')", oc, oc.getRoot());
        String name1 = (String) Ognl.getValue("getName()", oc, oc.getRoot());

        String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());
複製代碼

(注意:可以將兩條語句寫在一起,逗號分隔,返回值是最後一個語句)

 

調用靜態方法:

        Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc,oc.getRoot());

 

創建對象:

複製代碼
        // 創建list對象
        Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
        String name1 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());
        String name2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot());

        // 創建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());
複製代碼

 

 OGNL表達式和Struts2結合原理:

OGNLContext本質上就是Struts2中的值棧(ValueStack

ValueStack中有兩部分:ROOT、CONTEXT

(隊列:先進先出、棧:先進後出)

ROOT部分是一個棧,CONTEXT部分是ActionContext

ROOT中的這個棧本質是一個容器(集合)

例如用list集合製作棧結構容器:push壓棧方法  list.add(0,a);、pop彈棧方法  list.remove(0),就可以模擬一個棧

 

Struts2中有一個類:CompoundRoot類就是一個棧,實現原理和上面類似

用棧的原因:在取棧中的屬性時候,會從棧頂開始找屬性,找不到繼續向下找,找到停止

默認情況下棧中放置的是當前訪問的Action對象

 

OGNL表達式和Struts2結合的體現:

Struts2有一個攔截器params,作用是將獲得的參數交給OGNL處理

學習java到知海匠庫互聯網學院

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章