1.介绍和准备
OGNL:对象视图导航语言。${user.addr.name},这种写法就叫对象视图导航OGNL不仅仅可以视图导航,支持EL表达式更加丰富的功能。(在使用OGNL表达式之前需要导入相应的jar包)
图1、OGNL表达式(EL为复习内容,与OGNL无关)
图2、项目结构
2.测试
2.1取出root中的user对象的name属性(@Test需要引入import org.junit.Test;然后在运行时直接点击Run as JUnit Test)
@Test
public void fun1() throws OgnlException {
//准备OGNLContext
//准备Root
User rootUser1 = new User("tom",18);
//准备Context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("isleiyi",18));
context.put("user2", new User("Rose",19));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser1);
oc.setValues(context);
//书写OGNl
//取出root中user对象的name属性
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
System.out.println(name);
}
输出:tom
2.2取出root中的属性值
@Test
//取出root中的属性值
public void fun2() throws OgnlException {
//准备OGNLContext
//准备Root
User rootUser1 = new User("tom",18);
//准备Context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("isleiyi",18));
context.put("user2", new User("Rose",19));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser1);
oc.setValues(context);
//书写OGNl
//取出root中user对象的name属性
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
System.out.println(name+age);
}
输出:tom18
2.3取出context中的属性值
@Test
//取出context中的属性值
public void fun3() throws OgnlException {
//准备OGNLContext
//准备Root
User rootUser1 = new User("tom",18);
//准备Context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("isleiyi",18));
context.put("user2", new User("Rose",19));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser1);
oc.setValues(context);
//书写OGNl
//取出root中user对象的name属性
//#从context中取值,user1从contextMap中取出user1这个键对应的值(user1这个对象的属性)
String name1 = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
Integer age1 = (Integer) Ognl.getValue("#user1.age", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());
Integer age2 = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());
System.out.println(name1+age1);
System.out.println(name2+age2);
}
输出:
isleiyi18
Rose19
2.4为属性赋值
@Test
//为属性赋值
public void fun4() throws OgnlException {
//准备OGNLContext
//准备Root
User rootUser1 = new User("tom",18);
//准备Context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("isleiyi",18));
context.put("user2", new User("Rose",19));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser1);
oc.setValues(context);
//书写OGNl
//取出root中user对象的name属性
//#从context中取值,user1从contextMap中取出user1这个键对应的值(user1这个对象的属性)
Ognl.getValue("name='isqian'", oc, oc.getRoot());
String name1 = (String) Ognl.getValue("name", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("#user1.name='qianqian'", oc, oc.getRoot());
System.out.println(name1);
System.out.println(name2);
}
输出:isqian
qianqian
2.5调用getName()方法
@Test
//调用方法
public void fun5() throws OgnlException {
//准备OGNLContext
//准备Root
User rootUser1 = new User("tom",18);
//准备Context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("isleiyi",18));
context.put("user2", new User("Rose",19));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser1);
oc.setValues(context);
//书写OGNl
//取出root中user对象的name属性
//#从context中取值,user1从contextMap中取出user1这个键对应的值(user1这个对象的属性)
String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());
System.out.println(name);
}
输出:tom
2.6调用setName()方法
@Test
//调用方法
public void fun6() throws OgnlException {
//准备OGNLContext
//准备Root
User rootUser1 = new User("tom",18);
//准备Context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("isleiyi",18));
context.put("user2", new User("Rose",19));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser1);
oc.setValues(context);
//书写OGNl
//取出root中user对象的name属性
//#从context中取值,user1从contextMap中取出user1这个键对应的值(user1这个对象的属性)
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());
System.out.println(name1);
System.out.println(name2);
}
输出:
李雷
lucy
2.7调用静态方法、调用Math的PI
@Test
//调用静态方法
public void fun7() throws OgnlException {
//准备OGNLContext
//准备Root
User rootUser1 = new User("tom",18);
//准备Context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("isleiyi",18));
context.put("user2", new User("Rose",19));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser1);
oc.setValues(context);
//书写OGNl
//取出root中user对象的name属性
//#从context中取值,user1从contextMap中取出user1这个键对应的值(user1这个对象的属性)
String name1 = (String) Ognl.getValue("@com.aisino.a_ognl.TestUtils@echo('hello leiyi')", oc, oc.getRoot());
Double PI1 = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
Double PI2 = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
System.out.println(name1);
System.out.println(PI1);
System.out.println(PI2);
}
输出:
hello leiyi
3.141592653589793
3.141592653589793