struts2 OGNL的用法介绍(二)
n person1 = new Person();
person1.setName("zhangsan");
Person person2 = new Person();
person2.setName("lisi");
Person person3 = new Person();
person3.setName("wangwu");
/* person4不放入到上下文环境中*/
Person person4 = new Person();
person4.setName("zhaoliu");
/* 将person1、person2、person3添加到环境中(上下文中)*/
context.put("person1", person1);
context.put("person2", person2);
context.put("person3", person3);
try
{
/* 获取根对象的"name"属性值*/
Object value = Ognl.getValue("name", context, person2);
System.out.println("ognl expression \"name\" eva luation is : " + value);
/* 获取根对象的"name"属性值*/
Object value2 = Ognl.getValue("#person2.name", context, person2);
System.out.println("ognl expression \"#person2.name\" eva luation is : " + value2);
/* 获取person1对象的"name"属性值*/
Object value3 = Ognl.getValue("#person1.name", context, person2);
System.out.println("ognl expression \"#person1.name\" eva luation is : " + value3);
/* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中*/
Object value4 = Ognl.getValue("name", context, person4);
System.out.println("ognl expression \"name\" eva luation is : " + value4);
/* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中*/
Object value5 = Ognl.getValue("#person4.name", context, person4);
System.out.println("ognl expression \"person4.name\" eva luation is : " + value5);
/* 获取person4对象的"name"属性,注意person4对象不在上下文中*/
// Object value6 = Ognl.getValue("#person4.name", context, person2);
// System.out.println("ognl expression \"#person4.name\" eva luation is : " + value6);
}
2)使用OGNL调用方法
public static void main(String[] args)
{
/* OGNL提供的一个上下文类,它实现了Map接口*/
OgnlContext context = new OgnlContext();
People people1 = new People();
people1.setName("zhangsan");
People people2 = new People();
people2.setName("lisi");
People people3 = new People();
people3.setName("wangwu");
context.put("people1", people1);
context.put("people2", people2);
context.put("people3", people3);
context.setRoot(people1);
try
{
/* 调用 成员方法*/
Object value = Ognl.getValue("name.length()", context, context.getRoot());
System.out.println("people1 name length is :" + value);
Object upperCase = Ognl.getValue("#people2.name.toUpperCase()", context, context.getRoot());
System.out.println("people2 name upperCase is :" + upperCase);
Object invokeWithArgs = Ognl.getValue("name.charAt(5)", context, context.getRoot());
System.out.println("people1 name.charAt(5) is :" + invokeWithArgs);
/* 调用静态方法*/
Object min = Ognl.getValue("@java.lang.Math@min(4,10)", context, context.getRoot());
System.out.println("min(4,10) is :" + min);
/* 调用静态变量*/
Object e = Ognl.getValue("@java.lang.Math@E", context, context.getRoot());
System.out.println("E is :" + e);
}
3)使用OGNL操作集合
public static void main(String[] args) throws Exception
{
OgnlContext context = new OgnlContext();
Classroom classroom = new Classroom();
classroom.getStudents().add("zhangsan");
classroom.getStudents().add("lisi");
classroom.getStudents().add("wangwu");
classroom.getStudents().add("zhaoliu");
classroom.getStudents().add("qianqi");
Student student = new Student();
student.getContactWay