一、 OGNL概述
OGNL(Object-Graph Navigation Language),是一种功能强大的开源表达式语言。可以通过某种表达式语法,存取java对象的任意属性,调用java对象的方法,同时能够自动实现必要的类型转换。
二、 OGNL的作用
struts2默认表达式语言就是OGNL,具有以下特点
- 支持对象方法调用。例如:objName methodName()。
- 支持类静态方法调用和值访问,表达式的格式为@[类全名(包括包路径)]@[方法名|值名]例如:@java.lang String@format(foo %s, bar')。
- 支持赋值操作和表达式串联
例如:price=100,discount=0.8,calculatePrice(),在方法中进行乘法计算会返回80。 - 访问OGNL 上下文(OGNL context)和 ActionContext
- 操作集合对象
三、 OGNL的要素
OGNL的操作实际上就是围绕着其结构的三要素而进行的,分别是表达式(Expression),根对象(Root Object)、上下文环境(Context)
- 表达式
OGNL会根据表达式去对象中取值。所有OGNL操作都是针对表达式解析后进行的。表明了此次OGNL操作需要执行的业务。表达式就是一个带有语法含义的字符串,这个字符串规定了操作的类型和操作的内容 - 根对象(root)
Root对象可以理解为OGNL的操作对象,表明对哪个对象进行操作。可以以任意一个对象为根,通过OGNL可以访问与这个对象关联的其他对象 - Context对象
OGNL还需要一个上下文环境。设置了Root对象,OGNL可以对Root对象进行取值或写值等操作。
四、 OGNL表达式语法
1. 准备元素
//准备OGNLContext
//准备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));
//书写OGNL
OgnlContext oc = new OgnlContext();
//将rootUser作为root部分
oc.setRoot(rootUser);
//将context这个Map作为Context部分
oc.setValues(context);
2.基本语法
取出root中user对象的属性
String name = (String) Ognl.getValue("name",oc,oc.getRoot());
Integer age = (Integer) Ognl.getValue("age",oc,oc.getRoot());
System.out.println(name+" "+age);
}
取出context中user对象的属性
String name = (String) Ognl.getValue("#user1.name",oc,oc.getRoot());
Integer age = (Integer) Ognl.getValue("#user2.age",oc,oc.getRoot());
System.out.println(name+" "+age);
}
为属性赋值
//为根元素赋值
String name1 = (String) Ognl.getValue("name='jerry'",oc,oc.getRoot());
//为context属性赋值
String name2 = (String) Ognl.getValue("#user1.name='harry'",oc,oc.getRoot());
System.out.println(name1+" "+name2);
}
调用root对象和context对象的方法
//调用根对象的setName方法
Ognl.getValue("setName('dbs')",oc,oc.getRoot());
//调用根对象的getName方法 ------此时应该打印出dbs
String name = (String) Ognl.getValue("getName()",oc,oc.getRoot());
String name2 = (String) Ognl.getValue("#user1.setName('小哥哥 '),#user1.getName()",oc,oc.getRoot());
System.out.println(name +" "+name2);
}
调用静态方法
String name = (String) Ognl.getValue("@com.aiit.ognl.TestUtils@echo('hello world')", oc, oc.getRoot());
Double pai = (Double) Ognl.getValue("@java.lang.Math@PI')", oc, oc.getRoot());
System.out.println(name+" "+pai);
}
创建集合对象
//创建List对象
Integer size = (Integer) Ognl.getValue("{'tom','jerry','rose'}.size()", oc, oc.getRoot());
String name = (String) Ognl.getValue("{'tom','jerry','rose'}[0]", oc, oc.getRoot());//tom
String name1 = (String) Ognl.getValue("{'tom','jerry','rose'}.get(1)", oc, oc.getRoot());//jerry
System.out.println(size+" "+name+" "+name1);
//创建map
Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':'18'}.size()", oc, oc.getRoot());//2
String name2 = (String) Ognl.getValue("#{'name':'tom','age':'18'}['name']", oc, oc.getRoot());//tom
Integer age = (Integer) Ognl.getValue("#{'name':'tom','age':'18'}.get('age')", oc, oc.getRoot());//2
System.out.println(size2+" "+name2+" "+age);
}
五、 值栈的概述
1. 概念
ValueStack是Struts的一个接口,OgnlValueStack是ValueStack的实现类,客户端发起一个请求struts2架构会创建一个action实例同时创建一个OgnlValueStack值栈实例,
OgnlValueStack贯穿整个Action生命周期,struts2中使用OGNL请求Action的参数封装成对象存储到值栈中,并通过OGNL表达式读取值栈中的对象属性值。
2. 值栈的内部结构
值栈和Ognl的结构一样。包含两个元素,root和context我们查看源码
CompoundRoot root;(源码是用ArrayList所做的一个栈)
transient Map<String, Object> context;
根元素设置为栈的原因是,在我们取栈的属性时,会从我们的栈顶开始寻找元素,找不到会依次往下寻找元素。如果找到就停止返回
我们写一个Demo来查看我们ValueStack中root和Context,分别装的是什么
第一步:随意书写一个Action类
public class Demo1Action extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("Demo1Action!!");
return SUCCESS;
}
}
第二步:使用jsp访问该Action类
<!-- 调试标签 -->
<s:debug></s:debug>
第三步:点开页面中的<debug>
标签查看内容
root元素
context元素
结论:默认情况下,我们的栈元素放置当前访问的Action对象。Context部分放的就是我们的ActionContext的数据域中心(request,response,ServletContext,requestScope…
六、 struts和ognl结合的具体体现
1. 参数接受原理
2. 与配置文件结合
如果我们进行重定向中携带一些参数
<action name="Demo3Action" class="com.aiit.shows.Demo4Action" method="execute" >
<result name="success" type="redirectAction" >
<param name="actionName">Demo1Action</param>
<param name="namespace">/</param>
<!-- 如果添加的参数struts"看不懂".就会作为参数附加重定向的路径之后.
如果参数是动态的.可以使用${}包裹ognl表达式.动态取值
-->
<param name="name">${name}</param>
</result>
</action>
3. struts2标签
七、 拓展request的getAttribute方法
先从原生request域
第二查找ValueStack的栈(root)部分
查找ValueStack的Context部分(ActionContext)