访问Struts 2的命名对象:parameters、request、session、application和attr
| #parameters.greeting | ActionContext.getContext().getParameters().get("greeting") | 欢迎访问程序员之家 | |
| #request.empName | 张三 | ((Map)ActionContext.getContext().get("request")).get("empName") | 张三 |
| #session.empName | 李四 | ActionContext.getContext().getSession().get("empName") | 李四 |
| #application.empName | 王五 | ActionContext.getContext().getApplication().get("empName") | 王五 |
| #attr.empName | 张三 | ((Map)ActionContext.getContext().get("attr")).get("empName") | 张三 |
| emps[0].name | 张三 | ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getEmps().get(0).getName() | 张三 |
| emps[1].address | Address [city=上海, country=中国, street=南京西路20号] | ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getEmps().get(1).getAddress() | Address [city=上海, country=中国, street=南京西路20号] |
| emps[2].address.city | 成都 | ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getEmps().get(2).getAddress().getCity() | 成都 |
| emps.size | 3 | ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getEmps().size() | 3 |
| emps.isEmpty | false | ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getEmps().isEmpty() | false |
| emps[0].emails[0] | zhangsan@sina.com | ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getEmps().get(0).getEmails()[0] | zhangsan@sina.com |
| emps[1].emails[1] | lisi@163.com | ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getEmps().get(1).getEmails()[1] | lisi@163.com |
| emps[0].accounts['建行'] | 12345678 | ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getEmps().get(0).getAccounts().get("建行") | 12345678 |
| emps[1].accounts.农行 | 597432 | ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getEmps().get(1).getAccounts().get("农行") | 597432 |
| emps[2].accounts.size | 2 | ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getEmps().get(1).getAccounts().size() | 2 |
| emps[2].accounts.isEmpty | false | ((OgnlAction)ActionContext.getContext().getValueStack().peek()).getEmps().get(1).getAccounts().isEmpty() | false |
选择和投影
选择示例: emps.{?#this.salary > 2000}
- 李四的薪水是2200.0
- 王五的薪水是2600.0
投影示例: emps.{name}
- 张三
- 李四
- 王五
Lambda表达式
Lambda表达式示例:#isManager = :[#this.name == '张三' ? '是管理者' : '是雇员']
- 张三是管理者
- 李四是雇员
- 王五是雇员

本文详细介绍了如何在Struts2框架中使用OGNL表达式来访问不同的命名对象,包括parameters、request、session、application等。此外,还展示了如何通过OGNL表达式操作集合类型的数据结构,如列表、数组和Map,并提供了选择和投影操作的具体实例。

159

被折叠的 条评论
为什么被折叠?



