struts2架構學習筆記5:OGNL運算式,struts2ognl

來源:互聯網
上載者:User

struts2架構學習筆記5:OGNL運算式,struts2ognl

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類:

package bean;public class User {    private String name;    private Integer age;                public User() {        super();        // TODO Auto-generated constructor stub    }    public User(String name, Integer age) {        super();        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    }
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處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.