Struts2值栈的理解
数据传输背后机制:ValueStack(值栈)
在这一切的背后,是因为有了ValueStack(值栈)!
ValueStack基础:OGNL
要了解ValueStack,必须先理解OGNL(Object Graphic Navigatino Language)!
OGNL是Struts2中使用的一种表达式语言,它可以用于JSP的标签库中,以便能够方便的访问各种对象的属性;它用于界面将参数传递到Action(并进行类型转换)中;它还可以用于struts2的配置文件中!所以,非常有必要理解OGNL的基本机制。
Root对象
OGNL称为对象图导航语言。所谓对象图,即以任意一个对象为根,通过OGNL可以访问与这个对象关联的其它对象。如:
package cn.com.leadfar.struts2.actions; public class User { private String username; private Group group; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; }} package cn.com.leadfar.struts2.actions; public class Group { private String name; private Organization org; public String getName() { return name; } public void setName(String name) { this.name = name; } public Organization getOrg() { return org; } public void setOrg(Organization org) { this.org = org; }} package cn.com.leadfar.struts2.actions; public class Organization { private String orgId; public String getOrgId() { return orgId; } public void setOrgId(String orgId) { this.orgId = orgId; }}
User user = new User(); Group g = new Group(); Organization o = new Organization(); o.setOrgId("ORGID"); g.setOrg(o); user.setGroup(g);
//用JAVA来导航访问user.getGroup().getOrg().getOrgId();
//利用OGNL表达式访问 String value = (String)Ognl.getValue("group.org.orgId", user);
public void testOgnl01() throws Exception{ User user = new User(); user.setUsername("张三"); //利用OGNL表达式访问user对象的username属性 String value = (String)Ognl.getValue("username", user); log(value); } public void testOgnl02() throws Exception{ User user = new User(); Group g = new Group(); Organization o = new Organization(); o.setOrgId("ORGID"); g.setOrg(o); user.setGroup(g); //用JAVA来导航访问 log(user.getGroup().getOrg().getOrgId()); //利用OGNL表达式访问 String value = (String)Ognl.getValue("group.org.orgId", user); log(value); } public void testOgnl03() throws Exception{ User user = new User(); Group g = new Group(); Organization o = new Organization(); o.setOrgId("ORGID"); g.setOrg(o); user.setGroup(g); //用JAVA来导航访问 log(user.getGroup().getOrg().getOrgId()); //也可以在表达式中使用#root来代表root对象 String value = (String)Ognl.getValue("#root.group.org.orgId", user); log(value); } private void log(Object o){ System.out.println(o); }
public void testOgnl04() throws Exception{ User user = new User(); user.setUsername("张三"); Group g = new Group(); Organization o = new Organization(); o.setOrgId("ORGID"); g.setOrg(o); user.setGroup(g); User user2 = new User(); user2.setUsername("李四"); Map context = new HashMap(); context.put("u1", user); context.put("u2", user2); //在表达式中需通过“#+对象的名称”来访问context中的对象 //如果表达式中没有用到root对象,那么可以用任意一个对象代表root对象! String value = (String)Ognl.getValue("#u1.username + ',' + #u2.username", context,new Object()); log(value); } public void testOgnl05() throws Exception{ User user = new User(); user.setUsername("张三"); Group g = new Group(); Organization o = new Organization(); o.setOrgId("ORGID"); g.setOrg(o); user.setGroup(g); User user2 = new User(); user2.setUsername("李四"); User user3 = new User(); user3.setUsername("王五"); Map context = new HashMap(); context.put("u1", user); context.put("u2", user2); //给OGNL传递root对象及context对象,以便解释对应的表达式 String value = (String)Ognl.getValue("#u1.username + ',' + #u2.username + ',' + username", context,user3); log(value); }
public void testOgnl06() throws Exception{ User user = new User(); //调用setValue()方法来进行赋值 //第一个参数:OGNL表达式 //第二个参数:root对象 //第三个参数:要赋的值 Ognl.setValue("username", user, "张三"); log(user.getUsername()); } public void testOgnl07() throws Exception{ User user = new User(); Map context = new HashMap(); context.put("u", user); //调用setValue()方法来进行赋值 //第一个参数:OGNL表达式 //第二个参数:context对象 //第三个参数:root对象 //第四个参数:要赋的值 Ognl.setValue("#u.username", context, new Object(), "张三"); log(user.getUsername()); } public void testOgnl08() throws Exception{ User user = new User(); Map context = new HashMap(); context.put("u", user); //利用赋值符号"="来进行赋值 Ognl.getValue("#u.username = '李四'", context, new Object()); log(user.getUsername()); } public void testOgnl09() throws Exception{ User user1 = new User(); User user2 = new User(); Map context = new HashMap(); context.put("u1", user1); context.put("u2", user2); //在一个表达式中可以用逗号分隔,同时执行多个表达式 Ognl.getValue("#u1.username = '李四',#u2.username='王五'", context, new Object()); log(user1.getUsername()); log(user2.getUsername()); }
// public void testOgnl10() throws Exception{ User user = new User(); //如果是调用root对象的方法,可以直接使用方法的名称来调用方法 Integer value = (Integer)Ognl.getValue("addSomething(1,1)", user); log(value); } public void testOgnl11() throws Exception{ User user = new User(); user.setUsername("李四"); //如果是调用root对象的方法,可以直接使用方法的名称来调用方法 String value = (String)Ognl.getValue("getUsername()", user); log(value); } public void testOgnl12() throws Exception{ User user = new User(); Ognl.getValue("setUsername('王五')", user); String value = (String)Ognl.getValue("getUsername()", user); log(value); } // public void testOgnl13() throws Exception{ User user = new User(); user.setUsername("王五"); //调用静态变量 //注意:out是System中的静态变量,out是PrintStream类型的一个对象 //而println()则是out这个对象中的实例方法(不是静态方法) //调用静态方法,需要在类名和变量名前面加上@来调用,对于实例方法,用"."来调用 Ognl.getValue("@System@out.println(username)", user); } public void testOgnl14() throws Exception{ User user = new User(); user.setUsername("wangwu"); //调用静态方法,注意使用全路径类名 Ognl.getValue("@System@out.println(@cn.com.leadfar.utils.Utils@toUpperCase(username))", user); }
public void testOgnl15() throws Exception{ Object root = new Object(); Map context = new HashMap(); //利用OGNL创建java.util.List对象 List list = (List)Ognl.getValue("{123,'xxx','kdjfk'}", context, root); context.put("list", list); //利用OGNL创建数组 int[] intarray = (int[])Ognl.getValue("new int[]{23,45,67}", context, root); context.put("intarray", intarray); //利用OGNL表达式创建java.util.Map对象 Map mapvalue = (Map)Ognl.getValue("#{'listvalue':#list,'intvalue':#intarray}", context, root); context.put("mapvalue", mapvalue); //利用OGNL表达式访问这些数组和集合对象 Ognl.getValue("@System@out.println(#list[1])", context,root); Ognl.getValue("@System@out.println(#intarray[2])", context,root); Ognl.getValue("@System@out.println(#mapvalue.listvalue[0])", context,root); Ognl.getValue("@System@out.println(#mapvalue['intvalue'][0])", context,root); } public void testOgnl16() throws Exception{ List root = new ArrayList(); User user1 = new User(); user1.setUsername("张三"); User user2 = new User(); user2.setUsername("李四"); root.add(user1); root.add(user2); //如果root对象是List类型 log(Ognl.getValue("#root[0].username", root)); log(Ognl.getValue("#root[1].username", root)); }
void setValue(String expr, Object value);Object findValue(String expr);
public class CompoundRoot extends ArrayList { public CompoundRoot() { } public CompoundRoot(List list) { super(list); } public CompoundRoot cutStack(int index) { return new CompoundRoot(subList(index, size())); } public Object peek() { return get(0); } public Object pop() { return remove(0); } public void push(Object o) { add(0, o); }}
public class UserAction { private String username; private Integer age; private boolean valid; //查看用户的详细信息 public String detail(){ username = "张三"; age = 18; valid = true; return "detail"; }
username:<s:property value="username"/> <br/>valid:<s:property value="valid"/> <br/>age:<s:property value="age"/> <br/>
public class UserAction { private String username; private String name; //查看用户的详细信息 public String detail(){ username = "张三"; name = "王五"; User u = new User(); u.setUsername("赵毅"); ActionContext.getContext().getValueStack().push(u); return "detail"; }
public class UserAction { private String username; //查看用户的详细信息 public String detail(){ username = "张三"; List list = new ArrayList(); for(int i=0; i<10; i++){ User user = new User(); user.setUsername("User"+i); list.add(user); } ActionContext.getContext().put("users", list); User u = new User(); u.setUsername("赵毅"); ActionContext.getContext().getValueStack().push(u); return "detail"; }
1: <s:property value="username"/> <br/>2: <s:iterator value="#users">3: <s:property value="username"/>4: <s:property value="#root[2].username"/><br/>5: </s:iterator>6: <s:property value="username"/>7: <s:property value="#root[1].username"/> <!-- 张三 -->