一:ognl简介
1,OGNL的全称是Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式语言
2,OGNL相当于一个上下文(OgnlContext)概念,就是一个Map结构(教室 老师 学生)
它实现了java.utils.Map 的接口。Struts框架默认就支持ognl表达式语言。(从struts项目必须引入ognl.jar包可以看出)
比如:Map 教室
OgnlContext=根对象(1)+非根对象(N)
老师:跟对象 1
学生:非根对象 n
根对象只能有一个,而非根对象可以有多个, 非根对象要通过"#key"访问,根对象可以省略"#key"直接通过根对象属性就可以封装两个实体类进行测试
二:测试
1、工具类:
复制代码
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
55package com.xly.test; import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException; /** * 用于OGNL表达计算的一个工具类 * */ public class OnglExpression { private OnglExpression() { } /** * 根据OGNL表达式进行取值操作(通过表达式在上下文取对象获取值) * * @param expression * ognl表达式 * @param ctx * ognl上下文 * @param rootObject * ognl根对象 * @return */ public static Object getValue(String expression, OgnlContext ctx, Object rootObject) { try { return Ognl.getValue(expression, ctx, rootObject); } catch (OgnlException e) { throw new RuntimeException(e); } } /** * 根据OGNL表达式进行赋值操作 * * @param expression * ognl表达式 * @param ctx * ognl上下文 * @param rootObject * ognl根对象 * @param value * 值对象 */ public static void setValue(String expression, OgnlContext ctx, Object rootObject, Object value) { try { Ognl.setValue(expression, ctx, rootObject, value); } catch (OgnlException e) { throw new RuntimeException(e); } } }
2、员工实体类:
复制代码
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
50package com.xly.test; public class Employee { private String name; private Address address; private Integer salary; public Employee() { super(); } public Employee(String name, Integer salary) { super(); this.name = name; this.salary = salary; } public Integer getSalary() { return salary; } public void setSalary(Integer salary) { this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Employee [name=" + name + ", address=" + address + ", salary=" + salary + "]"; } }
3、经理实体类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package com.xly.test; public class Manager { private String name; public Manager() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Manager [name=" + name + "]"; } }
4、测试类 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
56
57
58package com.xly.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、特点:值栈是符合先进后出的
demo2内容
复制代码
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
45package com.xly.second; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ognl.OgnlValueStack; import com.opensymphony.xwork2.util.ValueStack; public class Demo2 extends ActionContext{ /** * * 值栈的使用 * */ public String ognl1(String[] args) { // 栈:表示一个先进后出的数据结构 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 = new OgnlValueStack(); 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(); } }
按照特点控制台打印顺序为ww,ls,zs
2、 特点2:值栈取值从上至下取
解释:因为Employee类有salary属性,但是student类没有salary属性;此时张雇员在底部,小明在顶部;要打印salary从student类开始拿值,没有就继续往下直到拿到为止,所以就会有上面的结果,salary为张雇员的2000,但是name为小明同学
其中ognl结构图
最后
以上就是神勇树叶最近收集整理的关于Struts之ognl一:ognl简介二:测试 三,值栈的使用的全部内容,更多相关Struts之ognl一:ognl简介二:测试 三,值栈内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复