目录
ognl简介
ognl 对象视图导航语言 Object graphic navigation language。
${user.addr.name} 这种写法就叫做对象视图导航。
OGNL不仅仅可以视图导航,还支持比EL表达式更加丰富的功能。
使用OGNL准备工作
导包(在struts框架中已经包含了此包不需要导入)。
EL表达式的取值对象
ognl表达式 ognlcontext ognl上下文中包含
1.ROOT 存入任何对象都可以
2.Context 只能存入map对象
建立一个user类
package cn.ycsj.entity;
public class User {
private String name;
private Integer age;
public User(){}
public static void echo(String aa){
System.out.println("静态方法" + aa);
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
1.创建ognlcontext并取出对象属性。
@Test
@Deprecated
//基本语法演示
public void func1() throws OgnlException {
//准备ognlcontext
//准备root
User rootuser = new User("zhangsan",15);
//准备Context
Map<String,User> context = new HashMap<>();
context.put("user1",new User("tom",15));
context.put("user2",new User("jack",10));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootuser);
oc.setValues(context);
//书写OGNL
//取出root中user对象 直接写属性名
String name = (String) Ognl.getValue("name",oc,oc.getRoot());
System.out.println(name);
//取出context中的属性值 #代表中context中取出
name = (String) Ognl.getValue("#user1.name",oc,oc.getRoot());
System.out.println("context :name=" + name);
}
2.ognl为属性赋值
@Test
@Deprecated
//基本语法演示 为属性赋值
public void func2() throws OgnlException {
//准备ognlcontext
//准备root
User rootuser = new User("zhangsan",15);
//准备Context
Map<String,User> context = new HashMap<>();
context.put("user1",new User("tom",15));
context.put("user2",new User("jack",10));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootuser);
oc.setValues(context);
//书写OGNL
//取出root中user对象 直接写属性名 为root赋值
Ognl.getValue("name='jerry'",oc,oc.getRoot());
String name = (String) Ognl.getValue("name",oc,oc.getRoot());
System.out.println(name);
//取出context中的属性值 #代表中context中取出
//里面的表达式能够串联Ognl.getValue("#user1.name='lalala',#user1.name='xxx'",oc,oc.getRoot());
// 取值只能取出最后一个,赋值可以串联
name = (String) Ognl.getValue("#user1.name='lalala'",oc,oc.getRoot());
System.out.println("context :name=" + name);
}
3.ognl调用对象中的普通方法
@Test
@Deprecated
//基本语法演示 调用对象中的普通方法。
public void func3() throws OgnlException {
//准备ognlcontext
//准备root
User rootuser = new User("zhangsan",15);
//准备Context
Map<String,User> context = new HashMap<>();
context.put("user1",new User("tom",15));
context.put("user2",new User("jack",10));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootuser);
oc.setValues(context);
//书写OGNL
//调用root中的方法
Ognl.getValue("setName('xxx')",oc,oc.getRoot());
String name = (String) Ognl.getValue("getName()",oc,oc.getRoot());
System.out.println(name);
//调用context键为user1对象的方法
Ognl.getValue("#user1.setName('yyy')",oc,oc.getRoot());
name = (String) Ognl.getValue("#user1.getName()",oc,oc.getRoot());
System.out.println("context :name=" + name);
}
4.ognl调用对象中的静态方法
@Test
@Deprecated
//基本语法演示 调用对象中的静态方法。
public void func4() throws OgnlException {
//准备ognlcontext
//准备root
User rootuser = new User("zhangsan",15);
//准备Context
Map<String,User> context = new HashMap<>();
context.put("user1",new User("tom",15));
context.put("user2",new User("jack",10));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootuser);
oc.setValues(context);
//书写OGNL
//调用root中的方法
Ognl.getValue("@cn.ycsj.entity.User@echo('aa')",oc,oc.getRoot());
double pi = (double) Ognl.getValue("@java.lang.Math@PI",oc,oc.getRoot());
System.out.println(pi);
//调用context键为user1对象的方法
Ognl.getValue("#user1.setName('yyy')",oc,oc.getRoot());
String name = (String) Ognl.getValue("#user1.getName()",oc,oc.getRoot());
System.out.println("context :name=" + name);
}
5.ognl创建list|map对象
@Test
@Deprecated
//基本语法演示 创建对象 list| map
public void func5() throws OgnlException {
//准备ognlcontext
//准备root
User rootuser = new User("zhangsan",15);
//准备Context
Map<String,User> context = new HashMap<>();
context.put("user1",new User("tom",15));
context.put("user2",new User("jack",10));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootuser);
oc.setValues(context);
//书写OGNL
//创建一个list对象
Integer size = (Integer) Ognl.getValue("{'tom','jerry','rose','jack'}.size",oc,oc.getRoot());
//从list取出对象
String aa = (String) Ognl.getValue("{'tom','jerry','rose','jack'}[0]",oc,oc.getRoot());
String bb = (String) Ognl.getValue("{'tom','jerry','rose','jack'}.get(1)",oc,oc.getRoot());
System.out.println(size);
System.out.println(aa);
System.out.println(bb);
//创建map对象
Ognl.getValue("#{'name':'jim','age':15,'addr':'beijing','sex':'male'}",oc,oc.getRoot());
//取大小
Integer size2 = (Integer) Ognl.getValue("#{'name':'jim','age':15,'addr':'beijing','sex':'male'}.size",oc,oc.getRoot());
//取值
String addr = (String) Ognl.getValue("#{'name':'jim','age':15,'addr':'beijing','sex':'male'}['addr']",oc,oc.getRoot());
System.out.println(size2);
System.out.println("addr :" + addr);
}