1、OGNL表达式
代码示例
User.java
package cn.ctgu.bean;
public class User {
private String name;
private Integer 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;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
Demo.java
package cn.ctgu.ognl;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import cn.ctgu.bean.User;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
//展示OGNL语法
public class Demo {
//准备工作
@Test
public void fun1() throws OgnlException {
//准备ONGLContext
//准备Root
User rootUser=new User("tom",18);
//准备Context
Map<String,User>context=new HashMap<String,User>();
context.put("user1", new User("Jack",18));
context.put("user2", new User("Rose",22));
OgnlContext oc=new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL
Ognl.getValue("", oc, oc.getRoot());
}
//基本语法演示,取出root中的属性值
@Test
public void fun2() throws OgnlException {
//准备ONGLContext
//准备Root
User rootUser=new User("tom",18);
//准备Context
Map<String,User>context=new HashMap<String,User>();
context.put("user1", new User("Jack",18));
context.put("user2", new User("Rose",22));
OgnlContext oc=new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL,取出root中的属性值
//取出root中user对象的name属性
String name=(String) Ognl.getValue("name", oc, oc.getRoot());
//取出root中user对象的age属性
Integer age=(Integer)Ognl.getValue("age", oc, oc.getRoot());
System.out.println(name);
System.out.println(age);
}
//取出context中的属性值
//基本语法演示
@Test
public void fun3() throws OgnlException {
//准备ONGLContext
//准备Root
User rootUser=new User("tom",18);
//准备Context
Map<String,User>context=new HashMap<String,User>();
context.put("user1", new User("Jack",18));
context.put("user2", new User("Rose",22));
OgnlContext oc=new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL,取出context中的属性值
//取出context中user1对象的name属性
String name=(String) Ognl.getValue("#user1.name", oc, oc.getRoot());
System.out.println(name);
//取出context中user2对象的age属性
Integer age=(Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());
System.out.println(age);
}
//为属性赋值
@Test
public void fun4() throws OgnlException {
//准备ONGLContext
//准备Root
User rootUser=new User("tom",18);
//准备Context
Map<String,User>context=new HashMap<String,User>();
context.put("user1", new User("Jack",18));
context.put("user2", new User("Rose",22));
OgnlContext oc=new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL,为root中user对象的name属性赋值
Ognl.getValue("name='jerry'",oc,oc.getRoot());
String name=(String) Ognl.getValue("name", oc, oc.getRoot());
//书写OGNL,为context中user对象的name属性赋值
String name2=(String)Ognl.getValue("#user1.nam='张三',#user1.name", oc, oc.getRoot());//如果有多个返回值,只会返回最后一个值
System.out.println(name);
System.out.println(name2);
}
//调用非静态方法
@Test
public void fun5() throws OgnlException {
//准备ONGLContext
//准备Root
User rootUser=new User("tom",18);
//准备Context
Map<String,User>context=new HashMap<String,User>();
context.put("user1", new User("Jack",18));
context.put("user2", new User("Rose",22));
OgnlContext oc=new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL,调用root中的setName()和getName()方法
Ognl.getValue("setName('lilei')",oc, oc.getRoot());
String name=(String)Ognl.getValue("getName()", oc, oc.getRoot());
System.out.println(name);
//调用context中的setName()和getName()方法
Ognl.getValue("#user1.setName('lucy')",oc, oc.getRoot());
String name2=(String)Ognl.getValue("#user1.getName()", oc,oc.getRoot());
System.out.println(name2);
}
//调用静态方法
@Test
public void fun6() throws OgnlException {
//准备ONGLContext
//准备Root
User rootUser=new User("tom",18);
//准备Context
Map<String,User>context=new HashMap<String,User>();
context.put("user1", new User("Jack",18));
context.put("user2", new User("Rose",22));
OgnlContext oc=new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL,取出ognlUtils中的静态方法
String name=(String) Ognl.getValue("@cn.ctgu.ognl.ognlUtils@echo('hello')", oc, oc.getRoot());
Double pi=(Double)Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());//调用Math静态方法中的PI属性
//Double pi=(Double)Ognl.getValue("@@PI", oc, oc.getRoot());//调用Math静态方法中的PI属性,这样也可以
System.out.println(name);
System.out.println(pi);
}
//创建对象
@Test
public void fun7() throws OgnlException {
//准备ONGLContext
//准备Root
User rootUser=new User("tom",18);
//准备Context
Map<String,User>context=new HashMap<String,User>();
context.put("user1", new User("Jack",18));
context.put("user2", new User("Rose",22));
OgnlContext oc=new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL,创建list对象
Integer size=(Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
System.out.println(size);
//取出list中的第一个元素
String name=(String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());
//String name=(String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(0)", oc, oc.getRoot());//这种写法也行,和上面效果一样
System.out.println(name);
}
@Test
public void fun8() throws OgnlException {
//准备ONGLContext
//准备Root
User rootUser=new User("tom",18);
//准备Context
Map<String,User>context=new HashMap<String,User>();
context.put("user1", new User("Jack",18));
context.put("user2", new User("Rose",22));
OgnlContext oc=new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL,创建map对象
Integer size=(Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());
System.out.println(size);//注意size等于2,而不是1
//取出其中的name值
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(name);
System.out.println(age);
}
}
2、OGNL与Struts结合