ognl
一,应用场景
1,标签上:如: <s:property value="user.name ">
value属性中自动支持 ognl表达式
2,配置文件中: 如: <result type="redirect">/main.jsp?name=${name} </result>
3,表单中: 如: <input type="text" name="user.name "/>
二,获取值栈
1,普通属性/方法
对象的属性:<s:property value="user.username "/>
对象的普通方法:<s:property value="user.get() "/>
2.取Action方法
调用action中的静态方法:<s:property value="@vs1 @get() "/> --如果值栈中只有一个action就是用vs
调用action中的静态方法:<s:property value="@vs2 @get() "/>
3,集合
2.1List
获取List:<s:property value=" testList "/>
获取List中的某一个元素:<s:property value=" testList[0] "/>
//利用伪属性
获取List的大小:<s:property value=" testSet . size "/>
// 利用投影
获取List中的对象的username属性:<s:property value=" studentList.{username} "/>
获取List中的 第一个对象 的username属性:<s:property value=" studentList.{username}[0] "/>利用投影
// 利用投影 +选择
选择基础知识:
?:表示取出所有对象
^:表示开始,取第一个元素
获取List中成绩及格的对象:<s:property value=" studentList .{?#this.grade>=60} "/>
获取List中成绩及格的对象的username:<s:property value=" studentList .{?#this.grade>=60}.{username} "/>
获取List中成绩及格的第一个对象的username:<s:property value=" studentList .{?#this.grade>=60}.{username}[0] "/> ---返回String(数组越界也不报异常)
获取List中成绩及格的第一个对象的username:<s:property value=" studentList .{^#this.grade>=60}.{username} "/> ---返回一个list
获取List中成绩及格的第一个对象的username:<s:property value=" studentList .{^#this.grade>=60}.{username}.size "/> ---用来判断是否有元素
获取List中成绩及格的最后一个对象的username:<s:property value=" studentList .{$#this.grade>=60}.{username} "/>
获取List中成绩及格的第一个对象然后求大小:<s:property value=" studentList .{^#this.grade>=600}.{username}.size "/>
2.2Set
获取Set:<s:property value=" testSet "/>
获取Set中的某一个元素:<s:property value=" testSet[0] "/> ------- 错误
2.3Map
获取Map:<s:property value=" testMap "/>
获取Map中的某一个元素:<s:property value=" testMap['m1'] "/>
//利用伪属性
获取Map中所有的键:<s:property value=" testMap.keys "/>
获取Map中所有的值:<s:property value=" testMap.values "/>
三,获取非值栈(@包+类@方法or 属性)
注: 普通类的普通方法不给调( 只能调用普通类的静态方法 和静态属性)
1,JDK
JDK中的类的静态方法:<s:property value="@java.lang.Math@floor(44.56) "/>
JDK中的类的静态方法:<s:property value="@@floor(44.56) "/> (同上)
JDK中的类的静态方法:<s:property value="@java.util.Calendar@getInstance() "/>
2.普通类
普通类中的静态属性:<s:property value="@cn.yangzhibin.Constant@AAA "/>
普通类的构造方法:<s:property value="new cn.yangzhibin.Student('张三','北京大学',30).name "/>
四,获取request,session里的属性
获取request范围中的属性:<s:property value="#request.req "/>
获取session范围中的属性:<s:property value="#session.ses "/>
获取attr中的属性:<s:property value="#attr.aaa "/>
五,Lambda表达式
使用Lambda表达式计算阶乘:<s:property value="#f = :[#this==1?1:#this*#f(#this-1)] , #f(4)"/>
六,ognl中符号的使用
6.1,# 的用法( 可以取出堆栈上下文中的存放的对象 )
# :不仅可以读取堆栈上下文中的存放的对象,还可以读取在定义<s:bean>定义定义的对象
名称
作用
例子
parameters
包含当前 HTTP 请求参数的 Map
#parameters.id[0] 作用相当于 request.getParameter("id")
request
包含当前 HttpServletRequest 的属性 ( attribute) 的 Map
#request.userName 相当于 request.getAttribute("userName")
session
包含当前 HttpSession 的属性 ( attribute )的 Map
#session.userName 相当于 session.getAttribute("userName")
application
包含当前应用的 ServletContext 的属 性( attribute )的 Map
#application.userName 相当于 application.getAttribute("userName")
attr
用于按 request > session > application 顺序访问其属性 ( attribute )
#attr.userName 相当于 从上面所有的对象范围内取一遍,取范围最小的那个对象
6.2,% 的用法
%{},----直接找action
可以取出存在值堆栈中的Action对象 ,直接调用它的方法.
如:用**%{getText('key')}** 的方式可以拿出国际化信息
6.3 ,$ 的用法
用途一: 用于在国际化资源文件中,引用OGNL表达式
用途二: 在Struts 2配置文件中,引用OGNL表达式
七, 值栈(ValueStack对象)可以省写#
栈:先进后出
查看值栈中的信息:<s:debug></s:debug>
7.1生命周期
值栈的生命周期 == 一个request请求**(每一次请求产生一个值** 栈 )