三、Struts与OGNL
1、OGNL基本使用
OGNL:对象导航图语言/对象视图导航语言(Object Graph Navigation Language)
OGNL分为root与context区域,在Strtus2中,ValueStack就是OGNLContext
那么ValueStack中也就有root(栈)与context(ActionContext)
并且ActionContext与ValueStack是相互引用的,所以获取ValueStack的方式为:
ActionContext.getcontext().getValueStack();
root:存放当前Action对象(栈结构)
context:存放ActionContext
准备工作:
导包:struts2的默认包中已经包含了OGNL需要的jar包
public class OgnlDemo1 { //展示OGNL语法 @Test public void fun1() throws Exception { //准备OGNLContext //准备root User rootUser=new User("tom",18,new Date()); //准备context Map<String,User> context = new HashMap(); context.put("user1", new User("jack",20,new Date())); context.put("user2", new User("rose",22,new Date())); OgnlContext oc = new OgnlContext(); oc.setRoot(rootUser); oc.setValues(context); //书写OGNL //取root中user对象的name属性 String name=(String) Ognl.getValue("name", context, oc.getRoot()); System.out.println(name); int age=(int) Ognl.getValue("age", context,oc.getRoot()); System.out.println(age); //取出contxt中的属性 #代表要从取值 String name1=(String) Ognl.getValue("#user1.name", context, oc.getRoot()); System.out.println(name1); } //基本语法演示 //为属性赋值 @Test public void fun2() throws Exception { //准备OGNLContext //准备root User rootUser=new User("tom",18,new Date()); //准备context Map<String,User> context = new HashMap(); context.put("user1", new User("jack",20,new Date())); context.put("user2", new User("rose",22,new Date())); OgnlContext oc = new OgnlContext(); //将rootUser作为root部分 oc.setRoot(rootUser); //将context作为context部分 oc.setValues(context); //书写OGNL //OGNL修改值root Ognl.getValue("name='jerry'", context, oc.getRoot()); //取值 String name=(String)Ognl.getValue("name", context, oc.getRoot()); System.out.println(name); //OGNL修改context Ognl.getValue("#user1.name='张三',#user2.name='李四'",context, oc.getRoot()); String cname=(String) Ognl.getValue("#user1.name",context, oc.getRoot()); String cname1=(String)Ognl.getValue("#user2.name",context, oc.getRoot()); System.out.println(cname+"_"+cname1); //OGNL调用方法 System.out.println(Ognl.getValue("getName()", context, oc.getRoot())); System.out.println(Ognl.getValue("#user1.setName('lucy'),#user1.getName()", context, oc.getRoot())); //OGNL调用静态方法 使用 @符号完整类名加上@方法名 System.out.println(Ognl.getValue("@java.lang.Math@random()",context, oc.getRoot())); System.out.println(Ognl.getValue("@java.lang.Math@PI",context, oc.getRoot())); System.out.println(Ognl.getValue("@@PI",context, oc.getRoot())); } //OGNL创建对象 @Test public void fun3() throws Exception { OgnlContext oc = new OgnlContext(); int num=(int) Ognl.getValue("{'tom','jerry','lucy','rose'}.size()", oc,oc.getRoot()); String name=(String) Ognl.getValue("{'tom','jerry','lucy','rose'}[0]", oc,oc.getRoot()); String name1=(String) Ognl.getValue("{'tom','jerry','lucy','rose'}.get(0)", oc,oc.getRoot()); // System.out.println(num); // System.out.println(name); // System.out.println(name1); //创建Map集合 并取值 int num1=(int) Ognl.getValue("#{'name':'tom','age':18}.size()", oc,oc.getRoot()); String name2=(String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc,oc.getRoot()); int age=(int) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc,oc.getRoot()); System.out.println(num1); System.out.println(age); } }
2、OGNL与Struts2结合
在配置文件中使用OGNL动态获取资料参数,需要在Action设置对应属性
public class ValueStackDemo extends ActionSupport { private String name; private String address; @Override public String execute() throws Exception { ValueStack valueStack = ActionContext.getContext().getValueStack(); name="zhangsan"; address="重庆"; return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="ognl" extends="struts-default" namespace="/"> <action name="valueS" class="Ognl.ValueStackDemo"> <result name="success" type="redirectAction"> <param name="actionName">valueST</param> <param name="namespace">/</param> <param name="name">${name}</param> <param name="address">${address}</param> </result> </action> <action name="valueST" class="Ognl.ValueSTest"> </action> </package> </struts>
http://localhost:8080/Struts2_day01/valueST.action?name=zhangsan&address=重庆
3、Struts2中的request
由于request被struts2包装,在JSP页面获取ActionContext.getcontext().put(key,value)中的值的时候的获取顺序即request.getAttribute()的获取:
1、原生request域
2、查找ValueStack的栈部分(root)
3、查找ValueStack的context部分(ActionContext)
4、Struts2参数接受与OGNL
不管是属性驱动还是对象驱动还是模型驱动的方式获取参数,
其原理就是将获取到的页面参数放入栈中,再通过OGNL表达式赋值