OGNL(取值、赋值、调用普通方法、静态方法、创建对象)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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("#ername",ognlContext,ognlContext.getRoot());
Integer age1=(Integer)Ognl.getValue("#erage",ognlContext,ognlContext.getRoot());
System.out.println("⽤户名:"+name1+",年龄"+age1);
}
(1)在初始化部分,需要先对root和context分别做初始化操作,然后将root和context放⼊到OGNLContext对象内部,这样初始化⼯作就完成了。
(2)取值操作分为两种:从root中取值和从context中取值,从root中取值的时候不需要加“#”,只需要写⼊变量名即可;当从context中取值的时候需要先加上“#”。
例如:"#ername"中user1的作⽤是取出键为user1的对象,即:new User("user1","111",12)这⼀部分,表达式#ername,则是从对象中取出对象的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("#ername='zhao'",ognlContext,ognlContext.getRoot());
String name1= (String)Ognl.getValue("#ername",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("@ng.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集合不同,创建时需要在集合前⾯加上“#”,⽤键取值。