一、OGNL表达式
1.1 简介
OGNL(Object Graphic Navigator Language)叫做对象视图导航语言,类似这种写法:${user.addr.name} 有点对象导航着找东西的意思。
1.2 OGNL的使用
1.2.1 导包
sturts2中已经导入了相关的包,已经可以直接使用了。
1.2.2 代码准备
使用ognl需要一个root对象和一个context对象来共同完成,这也相当于ognl的接口定义了。
使用ognl表达式就是去访问这两个对象里的数据。
基础代码
public class Demo {
public static void main(String[] args) throws Exception {
//准备ognl的环境
//1.准备root对象
User userRoot = new User();
userRoot.setName("tom");
userRoot.setAge(18);
//2.准备context对象
HashMap<String, User> context = new HashMap<String,User>();
//构造ognl上下文
OgnlContext oc = new OgnlContext();
oc.setRoot(userRoot);
oc.setValues(context);
}
}
二、语法
2.1 基本取值
2.1.1 取根元素里边的值
取根元素里边的值可以直接写对象的属性名字就行了。
public class Demo {
public static void main(String[] args) throws Exception {
//准备ognl的环境
//1.准备root对象
User userRoot = new User();
userRoot.setName("tom");
userRoot.setAge(18);
//2.准备context对象
HashMap<String, User> context = new HashMap<String,User>();
//构造ognl上下文
OgnlContext oc = new OgnlContext();
oc.setRoot(userRoot);
oc.setValues(context);
//基本取值
String name = (String) Ognl.getValue("name",oc,oc.getRoot());
Integer age = (Integer) Ognl.getValue("age",oc,oc.getRoot());
System.out.println(name);
System.out.println(age);
}
}
2.1.2 取context中的值
@Test
public void fun1() throws Exception{
//准备ognl的环境
// 1.准备root对象
User userRoot = new User();
userRoot.setName("tom");
userRoot.setAge(18);
// 2.准备context对象
HashMap<String, User> context = new HashMap<String, User>();
context.put("user1", new User("zhangsan", 18));
context.put("user2", new User("lisi", 23));
// 构造ognl上下文
OgnlContext oc = new OgnlContext();
oc.setRoot(userRoot);
oc.setValues(context);
String name = (String) Ognl.getValue("#user1.name",oc,oc.getRoot());
Integer age = (Integer) Ognl.getValue("#user2.age",oc,oc.getRoot());
System.out.println(name);
System.out.println(age);
}
上述分别取键为user1的User对象中的name,键为user2的User对象中的age的值,打印如下:
2.2 赋值
public void fun2() throws Exception{
//准备ognl的环境
// 1.准备root对象
User userRoot = new User();
userRoot.setName("tom");
userRoot.setAge(18);
// 2.准备context对象
HashMap<String, User> context = new HashMap<String, User>();
context.put("user1", new User("zhangsan", 18));
context.put("user2", new User("lisi", 23));
// 构造ognl上下文
OgnlContext oc = new OgnlContext();
oc.setRoot(userRoot);
oc.setValues(context);
//直接赋值完就获取值
String name = (String) Ognl.getValue("name='jerry'",oc,oc.getRoot());
String name1 = (String) Ognl.getValue("name",oc,oc.getRoot());
//对context里边的User对象赋值
Integer age = (Integer) Ognl.getValue("#user2.age=35",oc,oc.getRoot());
System.out.println(name);
System.out.println(name1);
System.out.println(age);
}
对根元素的name属性进行赋值,然后再获取值,可以直接 接收返回值,也可以再次获取。
2.3 调用方法
public void fun3() throws Exception{
//准备ognl的环境
// 1.准备root对象
User userRoot = new User();
userRoot.setName("tom");
userRoot.setAge(18);
// 2.准备context对象
HashMap<String, User> context = new HashMap<String, User>();
context.put("user1", new User("zhangsan", 18));
context.put("user2", new User("lisi", 23));
// 构造ognl上下文
OgnlContext oc = new OgnlContext();
oc.setRoot(userRoot);
oc.setValues(context);
String name = (String) Ognl.getValue("setName('haha')",oc,oc.getRoot());
String name1 = (String) Ognl.getValue("getName()",oc,oc.getRoot());
Integer age = (Integer) Ognl.getValue("#user2.setAge(98)",oc,oc.getRoot());
Integer age1 = (Integer) Ognl.getValue("#user2.getAge()",oc,oc.getRoot());
System.out.println(name);
System.out.println(name1);
System.out.println(age);
System.out.println(age1);
}
调用根元素的setName方法进行赋值,调用getName()方法取值。下边是调用context中的user对象的相关方法。
可以看出调用方法,不会直接放回设置的值。
2.4 调用静态方法
在User类中定义一个静态方法
package cn.itcast.action;
public class User {
public static String sayHello(String msg){
System.out.println("接受到的数据:"+msg);
return msg;
}
}
调用静态方法,用@符号标记完全限定类名和方法名,可以接收返回值
public void fun4() throws Exception{
//准备ognl的环境
// 1.准备root对象
User userRoot = new User();
userRoot.setName("tom");
userRoot.setAge(18);
// 2.准备context对象
HashMap<String, User> context = new HashMap<String, User>();
context.put("user1", new User("zhangsan", 18));
context.put("user2", new User("lisi", 23));
// 构造ognl上下文
OgnlContext oc = new OgnlContext();
oc.setRoot(userRoot);
oc.setValues(context);
String msg = (String) Ognl.getValue("@cn.itcast.action.User@sayHello('i am static')",oc,oc.getRoot());
System.out.println(msg);
}
2.5 创建集合对象
public void fun5() throws Exception{
//准备ognl的环境
// 1.准备root对象
User userRoot = new User();
userRoot.setName("tom");
userRoot.setAge(18);
// 2.准备context对象
HashMap<String, User> context = new HashMap<String, User>();
context.put("user1", new User("zhangsan", 18));
context.put("user2", new User("lisi", 23));
// 构造ognl上下文
OgnlContext oc = new OgnlContext();
oc.setRoot(userRoot);
oc.setValues(context);
//创建list对象 并获取size大小和相关值
Integer size = (Integer) Ognl.getValue("{'jack','tom','rose','marry'}.size()",oc,oc.getRoot());
String name1 = (String) Ognl.getValue("{'jack','tom','rose','marry'}[0]",oc,oc.getRoot());
String name2 = (String) Ognl.getValue("{'jack','tom','rose','marry'}.get(1)",oc,oc.getRoot());
System.out.println(size);
System.out.println(name1);
System.out.println(name2);
}
直接创建了一个集合并调用了相关方法获取了数据
2.6 创建map对象
public void fun6() throws Exception{
//准备ognl的环境
// 1.准备root对象
User userRoot = new User();
userRoot.setName("tom");
userRoot.setAge(18);
// 2.准备context对象
HashMap<String, User> context = new HashMap<String, User>();
context.put("user1", new User("zhangsan", 18));
context.put("user2", new User("lisi", 23));
// 构造ognl上下文
OgnlContext oc = new OgnlContext();
oc.setRoot(userRoot);
oc.setValues(context);
//创建map对象 并获取size大小和相关值
Integer size = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()",oc,oc.getRoot());
String name = (String) Ognl.getValue("#{'name':'tom','age':18}['name']",oc,oc.getRoot());
Integer age = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')",oc,oc.getRoot());
System.out.println(size);
System.out.println(name);
System.out.println(age);
}
用#来进行区别
二、Struts2中的Ognl
这里我们简单介绍一下,struts2中的赋值取值方式就是ognl的方式,其中有个重要的对象ValueStack(值栈对象)就是类似ognl上下文的一个对象,里边有root对象和map对象。深入的我们就不在讲解了。这是struts2中的取值原理。
三、总结
今天我们学习了ognl,不得不说还是很强大的。