*******
1,
package junit.test; import java.util.HashMap; import java.util.Map; import ognl.Ognl; import ognl.OgnlException; import org.junit.Test; import com.maple.ognl.Animal; public class OGNLTest { @Test public void test1(){ Map<String,Object> context=new HashMap<String,Object>(); Animal a1=new Animal("rabit1"); Animal a2=new Animal("rabit2"); Animal a3=new Animal("rabit3"); Animal a4=new Animal("rabit4"); context.put("aaa1", a1); context.put("aaa2", a2); context.put("aaa3", a3); try { Object value1=Ognl.getValue("name", context, a1); System.out.println(value1); //rabit1 Object value2=Ognl.getValue("#aaa1.name", context,a1); System.out.println(value2);//rabit1 Object value3=Ognl.getValue("#aaa1.name", context, a2); System.out.println(value3);//rabit1
//将a4作为root对象,获取a4对象的name属性,a4不在上下文中 Object value4=Ognl.getValue("name", context, a4); System.out.println(value4);//rabit4 Object value5=Ognl.getValue("#aaa4.name", context, a4); //System.out.println(value5); //抛异常 } catch (OgnlException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
总结:对于使用上下文的ognl,若不指定从哪个对象中查找"name"属性,则默认从root对象中查找;
若指定了(通过#指定,如#aaa1,),则从指定对象中查找,如果指定对象不在上下文中,则ognlException异常,
2,调用成员变量,静态方法,静态变量
@Test public void test2(){ OgnlContext context=new OgnlContext(); Animal a1=new Animal("ra1"); Animal a2=new Animal("rabit2"); Animal a3=new Animal("rabi3"); context.put("aa1", a1); context.put("aa2", a2); context.put("aa3", a3); context.setRoot(a1); try { /*调用成员方法*/ Object value=Ognl.getValue("name.length()",context,context.getRoot()); System.out.println(value);//3 Object value2=Ognl.getValue("#aa2.name.toUpperCase()", context,context.getRoot()); System.out.println(value2); //RABIT2 Object value3=Ognl.getValue("#aa3.name.charAt(2)", context,context.getRoot()); System.out.println(value3); //b /*调用静态方法*/ Object value4=Ognl.getValue("@java.lang.Math@min(2,4)", context,context.getRoot()); System.out.println(value4);//2 Object value5=Ognl.getValue("@java.lang.Math@E", context,context.getRoot()); System.out.println(value5);//2.718281828459045 } catch (OgnlException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
成员方法调用:方法名+(),参数直接写里面;
静态方法调用:@类全名@方法名();
静态变量调用:@类全名@字段;
3,ognl操作集合对象
@Test public void test3(){ Classroom classroom=new Classroom(); classroom.getStudents().add("mary"); classroom.getStudents().add("lenka"); classroom.getStudents().add("linda"); classroom.getStudents().add("dan"); Student stu=new Student(); stu.getContactWays().put("cell", "111"); stu.getContactWays().put("phone", "222"); stu.getContactWays().put("mobile", "333"); OgnlContext context=new OgnlContext(); context.put("classroom", classroom); //list context.put("student", stu); //map context.setRoot(classroom); try { Object collection=Ognl.getValue("students", context, context.getRoot()); System.out.println(collection); //[mary, lenka, linda, dan] Object collection3=Ognl.getValue("students[0]", context, context.getRoot()); System.out.println(collection3); //mary Object collection4=Ognl.getValue("students.size()", context, context.getRoot()); System.out.println(collection4); //4 Object collection2=Ognl.getValue("#student.contactWays", context, context.getRoot()); System.out.println(collection2); //{phone=222, cell=111, mobile=333} Object collection5=Ognl.getValue("#student.contactWays['cell']", context, context.getRoot()); System.out.println(collection5); //111 Object collection6=Ognl.getValue("#student.contactWays['ff']", context, context.getRoot()); System.out.println(collection6); //null Object createcol=Ognl.getValue("{'aa','bb','cc'}", context, context.getRoot()); System.out.println(createcol);//[aa, bb, cc] Object createmap=Ognl.getValue("#{'k1':'v1','k2':'v2','k3':'v3'}", context, context.getRoot()); System.out.println(createmap); //{k1=v1, k2=v2, k3=v3} } catch (OgnlException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public class Classroom { private List<String> students=new ArrayList<String>(); public List<String> getStudents() { return students; } public void setStudents(List<String> students) { this.students = students; } } public class Student { private Map<String,Object> contactWays=new HashMap<String,Object>(); /*private Map<String,Object> contactWays=new LinkedHashMap<String,Object>();保持插入顺序*/ public Map<String, Object> getContactWays() { return contactWays; } public void setContactWays(Map<String, Object> contactWays) { this.contactWays = contactWays; } }
总结:ognl认为List和Array是一样的,创建List集合使用{},创建Map集合使用#{}
4,ognl投影和过滤集合
package com.maple.ognl; import java.util.ArrayList; import java.util.List; public class Human { private String name; private String sex; private int age; private List<Human> friends=new ArrayList<Human>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Human> getFriends() { return friends; } public void setFriends(List<Human> friends) { this.friends = friends; } public Human(String name, String sex, int age) { super(); this.name = name; this.sex = sex; this.age = age; } @Override public String toString() { return "Human [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } public Human(){} }
测试
//过滤集合,投影集合 @Test public void test4(){ OgnlContext context=new OgnlContext(); Human human=new Human(); human.setName("maple"); human.setSex("female"); human.setAge(22); human.getFriends().add(new Human("mary","fe",2)); human.getFriends().add(new Human("linda","fe",2)); human.getFriends().add(new Human("serlina","male",2)); context.put("human", human); context.setRoot(human); try { Object filter=Ognl.getValue("friends.{? #this.name.length()>4}", context,context.getRoot()); //[Human [name=linda, sex=fe, age=2], Human [name=serlina, sex=male, age=2]] System.out.println(filter); Object filter2=Ognl.getValue("friends.{name}", context,context.getRoot()); System.out.println(filter2);//[mary, linda, serlina] } catch (OgnlException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
总结:ognl可以对集合进行投影和过滤,就相当于数据库对表取列和取行操作;
过滤语法:collection.{? expression},#this表示集合当前对象
投影语法:collection.{expression}
5,ognl是怎样从VlaueStack中取值的
每当一个请求到达Action,Struts2会把Action对象压入栈中
<s:property value="username"/>
username:<%= (HelloWorldAction)ActionContext.getContext().getValueStack().peek().getUsername() %>
*******