OGNL(取值、赋值、调用普通方法、静态方法、创建对象)
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= (Value("username",Root());//取值
Integer age=(Value("userage",Root());
String password=(Value("password",Root());
System.out.println("⽤户名:"+name+",年龄"+age+",密码:"+password);同喜同喜
String name1= (Value("#user1.username",Root());
Integer age1=(Value("#user2.userage",Root());
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);
String name= (Value("username",Root());
System.out.println("⽤户名:"+name);
String name1= (Value("#user1.username",Root());
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);
String name=(String) Value("getUsername()",Root());
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);
String name=(String) Value("#Username()",Root());
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) Value("@l.Hello@nihao('zhai')",Root());        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) Value("@java.lang.Math@PI",Root());
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) Value("{'wu','han','jia','you'}[1]",Root());
String string2=(String) Value("{'wu','han','jia','you'}.get(1)",Root());
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) Value("#{'name':'zhao','age':12}['name']",Root());        System.out.println(string1);
}
与list集合不同,创建时需要在集合前⾯加上“#”,⽤键取值。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。