OGNL(取值、赋值、调用普通方法、静态方法、创建对象)

1、OGNL表达式

(1)概念:

OGNL:对象导航图语言(Object Graph Navigation Language),是一种表达式语言,功能比EL表达式更为强大,它是集成在Struts中的。

 在创建Struts项目的时候已经将OGNL有关的包导入了,所以,这里不需要重复导包。

(2)OGNLContext对象:

EL表达式从是一个内置对象中取值,而OGNL表达式只从OGNLContext对象中取值,该对象可以分为两部分,其中root部分可以存放任何对象,Context部分只能存放键值对。

2、OGNL初始化和取值

public class OgnlTest {
    public void test() throws OgnlException {
        User rootuser=new User("zhai","123",12);//root部分
        Map<String,User> context=new HashMap<String, User>();//context部分
        context.put("user1",new User("user1","111",12));
        context.put("user2",new User("user2","222",13));
        OgnlContext ognlContext=new OgnlContext();//创建OGNLContext对象
        ognlContext.setRoot(rootuser);
        ognlContext.setValues(context);//将root和context部分放入到OGNLContext内部
        String name= (String)Ognl.getValue("username",ognlContext,ognlContext.getRoot());//取值
        Integer age=(Integer)Ognl.getValue("userage",ognlContext,ognlContext.getRoot());
        String password=(String)Ognl.getValue("password",ognlContext,ognlContext.getRoot());
        System.out.println("用户名:"+name+",年龄"+age+",密码:"+password);
        String name1= (String)Ognl.getValue("#user1.username",ognlContext,ognlContext.getRoot());
        Integer age1=(Integer)Ognl.getValue("#user2.userage",ognlContext,ognlContext.getRoot());
        System.out.println("用户名:"+name1+",年龄"+age1);
    }

(1)在初始化部分,需要先对root和context分别做初始化操作,然后将root和context放入到OGNLContext对象内部,这样初始化工作就完成了。

(2)取值操作分为两种:从root中取值和从context中取值,从root中取值的时候不需要加“#”,只需要写入变量名即可;当从context中取值的时候需要先加上“#”。

例如:"#user1.username"中user1的作用是取出键为user1的对象,即:new User("user1","111",12)这一部分,表达式#user1.username,则是从对象中取出对象的username属性的值。

3、OGNL表达式的赋值

public void test1() throws OgnlException {
        User rootuser=new User("zhai","123",12);
        Map<String,User> context=new HashMap<String, User>();
        context.put("user1",new User("user1","111",12));
        context.put("user2",new User("user2","222",13));
        OgnlContext ognlContext=new OgnlContext();
        ognlContext.setRoot(rootuser);
        ognlContext.setValues(context);
Ognl.getValue(
"username='zhang'",ognlContext,ognlContext.getRoot()); String name= (String)Ognl.getValue("username",ognlContext,ognlContext.getRoot()); System.out.println("用户名:"+name); Ognl.getValue("#user1.username='zhao'",ognlContext,ognlContext.getRoot()); String name1= (String)Ognl.getValue("#user1.username",ognlContext,ognlContext.getRoot()); System.out.println("用户名:"+name1); }

用getValue()方法对元素的值进行修改,需要注意对root和context中的变量进行赋值的不同点。

4、调用方法

(1)root:

public void test2() throws OgnlException {
        User rootuser=new User("zhai","123",12);
        Map<String,User> context=new HashMap<String, User>();
        context.put("user1",new User("user1","111",12));
        context.put("user2",new User("user2","222",13));
        OgnlContext ognlContext=new OgnlContext();
        ognlContext.setRoot(rootuser);
        ognlContext.setValues(context);
        Ognl.getValue("setUsername('ZHAI')",ognlContext,ognlContext.getRoot());
        String name=(String) Ognl.getValue("getUsername()",ognlContext,ognlContext.getRoot());
        System.out.println("用户名:"+name);
    }

先动用User类中user对象的set方法,对user对象的username值进行修改,修改后在调用get方法取出对象的值。

(2)context:

public void test2() throws OgnlException {
        User rootuser=new User("zhai","123",12);
        Map<String,User> context=new HashMap<String, User>();
        context.put("user1",new User("user1","111",12));
        context.put("user2",new User("user2","222",13));
        OgnlContext ognlContext=new OgnlContext();
        ognlContext.setRoot(rootuser);
        ognlContext.setValues(context);
        Ognl.getValue("#user1.setUsername('ZHAI')",ognlContext,ognlContext.getRoot());
        String name=(String) Ognl.getValue("#user1.getUsername()",ognlContext,ognlContext.getRoot());
        System.out.println("用户名:"+name);
    }

 先用“#user1”调用对象user1,然后对user1对象进行赋值,在进行取值。

(3)调用自定义的静态方法:

先创建一个静态方法:

public class Hello {
    public static String nihao(String name){
        return "你好,"+name;
    }
}

用OGNL表达式调用静态方法:

    public void test3() throws OgnlException {
        User rootuser=new User("zhai","123",12);
        Map<String,User> context=new HashMap<String, User>();
        context.put("user1",new User("user1","111",12));
        context.put("user2",new User("user2","222",13));
        OgnlContext ognlContext=new OgnlContext();
        ognlContext.setRoot(rootuser);
        ognlContext.setValues(context);
        String string=(String) Ognl.getValue("@pers.zhb.ognl.Hello@nihao('zhai')",ognlContext,ognlContext.getRoot());
        System.out.println(string);
    }

 关键点是利用完整类名和静态方法名实现对方法的调用。

(4)调用已有的静态方法或属性:

public void test3() throws OgnlException {
        User rootuser=new User("zhai","123",12);
        Map<String,User> context=new HashMap<String, User>();
        context.put("user1",new User("user1","111",12));
        context.put("user2",new User("user2","222",13));
        OgnlContext ognlContext=new OgnlContext();
        ognlContext.setRoot(rootuser);
        ognlContext.setValues(context);
        Double aDouble= (Double) Ognl.getValue("@java.lang.Math@PI",ognlContext,ognlContext.getRoot());
        System.out.println(aDouble);
    }

 和调用自定义的静态方法或属性一样,只需完整类名和方法或属性名即可。

5、集合对象

(1)list集合:

    public void test4() throws OgnlException {
        User rootuser=new User("zhai","123",12);
        Map<String,User> context=new HashMap<String, User>();
        context.put("user1",new User("user1","111",12));
        context.put("user2",new User("user2","222",13));
        OgnlContext ognlContext=new OgnlContext();
        ognlContext.setRoot(rootuser);
        ognlContext.setValues(context);
        String string1=(String) Ognl.getValue("{'wu','han','jia','you'}[1]",ognlContext,ognlContext.getRoot());
        String string2=(String) Ognl.getValue("{'wu','han','jia','you'}.get(1)",ognlContext,ognlContext.getRoot());
        System.out.println(string1);
        System.out.println(string2);
    }

{'wu','han','jia','you'}为创建的一个list集合对象,以上程序为分别用两种方式获取list集合中的元素。
(2)map集合:
 public void test5() throws OgnlException {
        User rootuser=new User("zhai","123",12);
        Map<String,User> context=new HashMap<String, User>();
        context.put("user1",new User("user1","111",12));
        context.put("user2",new User("user2","222",13));
        OgnlContext ognlContext=new OgnlContext();
        ognlContext.setRoot(rootuser);
        ognlContext.setValues(context);
        String string1=(String) Ognl.getValue("#{'name':'zhao','age':12}['name']",ognlContext,ognlContext.getRoot());
        System.out.println(string1);
    }

与list集合不同,创建时需要在集合前面加上“#”,用键取值。

原文地址:https://www.cnblogs.com/zhai1997/p/12272925.html