一、ognl的简介
ognl的全称是Object Graph Navigation(对象图导航语言),
OgnlContext(ongl上下文)其实就是Map (教室、老师、学生)
OgnlContext=根对象(1)+非根对象(N)---》context英文原意是上下文,环境/容器
示例如下:
教室:Map
老师:根对象(1)
学生:非根对象(N)
二、Struts的传值的优先级
1、将需要的资料导入进去
2、Demo1
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56package com.lxy.test; import ognl.OgnlContext; import ognl.OgnlException; public class Demo1 { /** * @param args * @throws OgnlException */ public static void main(String[] args) { Employee e = new Employee(); e.setName("小李"); Manager m = new Manager(); m.setName("张经理"); // 创建OGNL下文,而OGNL上下文实际上就是一个Map对象 OgnlContext ctx = new OgnlContext(); // 将员工和经理放到OGNL上下文当中去 ctx.put("employee", e); ctx.put("manager", m); ctx.setRoot(e);// 设置OGNL上下文的根对象 /** ********************** 取值操作 *************************** */ // 表达式name将执行e.getName(),因为e对象是根对象(请注意根对象和非根对象表达式的区别) String employeeName = (String) OnglExpression.getValue("name", ctx, e); System.out.println(employeeName); // 表达式#manager.name将执行m.getName(),注意:如果访问的不是根对象那么必须在前面加上一个名称空间,例如:#manager.name String managerName = (String) OnglExpression.getValue("#manager.name", ctx, e); System.out.println(managerName); // 当然根对象也可以使用#employee.name表达式进行访问 employeeName = (String) OnglExpression.getValue("#employee.name", ctx, e); System.out.println(employeeName); /** ********************** 赋值操作 *************************** */ OnglExpression.setValue("name", ctx, e, "小明"); employeeName = (String) OnglExpression.getValue("name", ctx, e); System.out.println(employeeName); OnglExpression.setValue("#manager.name", ctx, e, "孙经理"); managerName = (String) OnglExpression.getValue("#manager.name", ctx, e); System.out.println(managerName); OnglExpression.setValue("#employee.name", ctx, e, "小芳"); employeeName = (String) OnglExpression.getValue("name", ctx, e); System.out.println(employeeName); } }
运行结果:
三、值栈
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46package com.lxy.test; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.ValueStack; public class Demo7 extends ActionSupport{ /** * * 值栈的使用 * */ public String ognl1() { // 栈:表示一个先进后出的数据结构 ValueStack vs = ActionContext.getContext().getValueStack(); // push方法把项压入栈顶 vs.push(new Employee("zs", 22)); vs.push(new Employee("ls", 22)); vs.push(new Employee("ww", 22)); // pop方法移除栈顶对象并作为此函数的值返回该对象 Employee e = (Employee) vs.pop(); System.out.println(e.getName()); e = (Employee) vs.pop(); System.out.println(e.getName()); e = (Employee) vs.pop(); System.out.println(e.getName()); return "bookEdit"; } /** * 此例用于模拟struts2的值栈计算过程 * * @param args */ public String ognl2() { ValueStack vs = ActionContext.getContext().getValueStack(); vs.push(new Employee("张雇员", 2000));// 1 vs.push(new Student("小明同学", "s001"));// 0 System.out.println(vs.findValue("name")); System.out.println(vs.findValue("salary2")); ActionContext ac = ActionContext.getContext(); return "bookEdit"; } }
配置Struts-sy.xml
<action name="/demo7_*" class="com.lxy.test.Demo7" method="{1}">
<result name="bookEdit">/bookEdit.jsp</result>
</action>
push方法把项压入栈顶
pop方法移出栈顶对象并作为此函数的值返回该对象
先进后出
值栈是从上到下直到取到为止
如: 取 name时,从上往下,因此取到了小明,而小明同学没有salary2,从上往下取,取到张
员的salary2
结构图
ValueStack为根对象
两者有同样的属性,在赋值的情况下,先看到谁就给谁赋值
最后
以上就是淡淡期待最近收集整理的关于Struts之Ognl一、ognl的简介二、Struts的传值的优先级 三、值栈的全部内容,更多相关Struts之Ognl一、ognl内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复