struts2(8)OGNL表達式

目錄

ognl簡介

1.創建ognlcontext並取出對象屬性。

2.ognl爲屬性賦值

3.ognl調用對象中的普通方法

4.ognl調用對象中的靜態方法

5.ognl創建list|map對象


 

ognl簡介

ognl  對象視圖導航語言 Object  graphic navigation language。

${user.addr.name} 這種寫法就叫做對象視圖導航。 

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

使用OGNL準備工作

導包(在struts框架中已經包含了此包不需要導入)。

EL表達式的取值對象

ognl表達式  ognlcontext  ognl上下文中包含

1.ROOT 存入任何對象都可以 

2.Context  只能存入map對象

建立一個user類

package cn.ycsj.entity;

public class User {
    private String name;
    private Integer age;
    public User(){}

    public static void echo(String aa){
        System.out.println("靜態方法" + aa);

    }

    public User(String name, Integer age) {
        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;
    }



}

1.創建ognlcontext並取出對象屬性。

    @Test
    @Deprecated
    //基本語法演示
    public void func1() throws OgnlException {
        //準備ognlcontext
            //準備root
        User rootuser = new User("zhangsan",15);

            //準備Context
        Map<String,User> context = new HashMap<>();
        context.put("user1",new User("tom",15));
        context.put("user2",new User("jack",10));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootuser);
        oc.setValues(context);
        //書寫OGNL
        //取出root中user對象  直接寫屬性名

        String name = (String) Ognl.getValue("name",oc,oc.getRoot());
        System.out.println(name);

        //取出context中的屬性值 #代表中context中取出
        name = (String) Ognl.getValue("#user1.name",oc,oc.getRoot());
        System.out.println("context :name=" + name);

    }

2.ognl爲屬性賦值

 @Test
    @Deprecated
    //基本語法演示  爲屬性賦值
    public void func2() throws OgnlException {
        //準備ognlcontext
        //準備root
        User rootuser = new User("zhangsan",15);

        //準備Context
        Map<String,User> context = new HashMap<>();
        context.put("user1",new User("tom",15));
        context.put("user2",new User("jack",10));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootuser);
        oc.setValues(context);
        //書寫OGNL
        //取出root中user對象  直接寫屬性名  爲root賦值

        Ognl.getValue("name='jerry'",oc,oc.getRoot());
        String name = (String) Ognl.getValue("name",oc,oc.getRoot());
        System.out.println(name);

        //取出context中的屬性值 #代表中context中取出
        //裏面的表達式能夠串聯Ognl.getValue("#user1.name='lalala',#user1.name='xxx'",oc,oc.getRoot());
        // 取值只能取出最後一個,賦值可以串聯
        name = (String) Ognl.getValue("#user1.name='lalala'",oc,oc.getRoot());
        System.out.println("context :name=" + name);

    }

3.ognl調用對象中的普通方法

@Test
    @Deprecated
    //基本語法演示  調用對象中的普通方法。
    public void func3() throws OgnlException {
        //準備ognlcontext
        //準備root
        User rootuser = new User("zhangsan",15);

        //準備Context
        Map<String,User> context = new HashMap<>();
        context.put("user1",new User("tom",15));
        context.put("user2",new User("jack",10));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootuser);
        oc.setValues(context);
        //書寫OGNL
        //調用root中的方法

        Ognl.getValue("setName('xxx')",oc,oc.getRoot());
        String name = (String) Ognl.getValue("getName()",oc,oc.getRoot());
        System.out.println(name);

        //調用context鍵爲user1對象的方法
        Ognl.getValue("#user1.setName('yyy')",oc,oc.getRoot());
        name = (String) Ognl.getValue("#user1.getName()",oc,oc.getRoot());
        System.out.println("context :name=" + name);

    }

4.ognl調用對象中的靜態方法

  @Test
    @Deprecated
    //基本語法演示  調用對象中的靜態方法。
    public void func4() throws OgnlException {
        //準備ognlcontext
        //準備root
        User rootuser = new User("zhangsan",15);

        //準備Context
        Map<String,User> context = new HashMap<>();
        context.put("user1",new User("tom",15));
        context.put("user2",new User("jack",10));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootuser);
        oc.setValues(context);
        //書寫OGNL
        //調用root中的方法

        Ognl.getValue("@cn.ycsj.entity.User@echo('aa')",oc,oc.getRoot());
        double pi = (double) Ognl.getValue("@java.lang.Math@PI",oc,oc.getRoot());
        System.out.println(pi);


        //調用context鍵爲user1對象的方法
        Ognl.getValue("#user1.setName('yyy')",oc,oc.getRoot());
        String name = (String) Ognl.getValue("#user1.getName()",oc,oc.getRoot());
        System.out.println("context :name=" + name);

    }

5.ognl創建list|map對象

@Test
    @Deprecated
    //基本語法演示  創建對象 list| map
    public void func5() throws OgnlException {
        //準備ognlcontext
        //準備root
        User rootuser = new User("zhangsan",15);

        //準備Context
        Map<String,User> context = new HashMap<>();
        context.put("user1",new User("tom",15));
        context.put("user2",new User("jack",10));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootuser);
        oc.setValues(context);
        //書寫OGNL

        //創建一個list對象
        Integer size = (Integer) Ognl.getValue("{'tom','jerry','rose','jack'}.size",oc,oc.getRoot());

        //從list取出對象
        String  aa = (String) Ognl.getValue("{'tom','jerry','rose','jack'}[0]",oc,oc.getRoot());

        String  bb = (String) Ognl.getValue("{'tom','jerry','rose','jack'}.get(1)",oc,oc.getRoot());

        System.out.println(size);
        System.out.println(aa);
        System.out.println(bb);

        //創建map對象
        Ognl.getValue("#{'name':'jim','age':15,'addr':'beijing','sex':'male'}",oc,oc.getRoot());
        //取大小
        Integer size2 = (Integer) Ognl.getValue("#{'name':'jim','age':15,'addr':'beijing','sex':'male'}.size",oc,oc.getRoot());
        //取值
        String addr = (String) Ognl.getValue("#{'name':'jim','age':15,'addr':'beijing','sex':'male'}['addr']",oc,oc.getRoot());

        System.out.println(size2);
        System.out.println("addr :" + addr);
    }

 

 

 

 

 

 

 

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