OGNL学习笔记

OGNL:
应用的场景:
标签中:
<s:property value="user.name">
配置文件中
<result type="redirect">/main.jsp?name=${name}</result>
调用值栈中对象的普通方法
<s:property value="user.get()">
调用Action中的静态方法 @
<s:property value="@cn.hhty.action.LoginAction@get()">

获取List集合:有序不重复
<s:property value="testList"/> 结果:[list1,list2,list3]
获取集合中的某一个元素
<s:property value="testList[0]">
(可以使用数组中的下标来获取List中的内容)

获取Set集合:无序可重复
获取Set集合
<s:property value="testSet"/> 结果:[set1,set2,set0]
获取Set中某一元素(由于Set无序,所有不能使用下标来取得)

获取Map集合
<s:property value="testMap"/>结果:[key1=value1,key2=value2]
获取Map中某一元素
<s:property value="testMap['key']">
获取大小
<s:property value="testMap.size">
返回map中所有的键:<s:property value="testMap.keys">
返回Map中所有的值:<s:property value="testMap.values">

获取List中所有的对象
<s:property value="listStudents">
利用投影获取List中对象的username属性值
<s:property value="listStudents.(username)">
利用投影获取List中第一个对象的username属性值
<s:property value="listStudents.(username)[0]">
利用投影获取List中成绩及格的对象
<s:property value="listStudents.(?#this.grade>=60).{username}[0]"/>

OGNL中#的使用:
#可以取出堆栈中上下文中存放的对象

[table]
|名称|作用|例子
|parameters|包含当前http请求参数的Map|#parameters.id[0]相当于request.getParameter("id");
|request|包含当前HttpServletRequest的属性(attribute)的Map|#request.userName相当于request.getRequest("userName");
|session|包含当前HttpSession的属性(attribute)的Map|#session.userName相当于session.getAttribute("userName");
|application|包含当前ServletContext的属性(attribute)的Map|#application.userName相当于application.getAttribute("userName");
|attr|依次request->session->application |
[/table]

后台:实现 RequestAware,SessionAware接口
Map request;
request.put("res","requestValue");
Map session;
session.put("ses","sessionValue");
前台获取:
获取request范围中的属性
<s:property value="#request.res">

获取Session范围中的属性值
<s:property value="#session.ses">

parameters方式
<action name="test" class="com.testAction">
<result type="redirect">/test.jsp?username=aabbcc</result>
</action>

在test.JSP里取值
获取Parameters对象的属性:(如果type!=redirect。则取不出来值)
<s:property value="#parameters.username">
注:默认的type是dispatcher, url后传的值前台取不到


OGNL中%的使用:
%可以取出存在值堆栈中的Action对象,直接调用它的方法
例如:如果Action继承了ActionSupport,那么在页面标签中
用%{getText('key')}的方式可以拿出国际化信息

%{}里面的内容表示是一个表达式,

$有两个主要用途:
1,用于在国际化资源文件中,引用OGNL表达式
2.在Struts2配置文件中,引用OGNL表达式 url?name=${name}


ValueStack对象,这个对象贯穿整个Action的生命周期(每个Action类的对象实例化会
有一个ValueStack对象),当Struts2接收到一个.action的请求后,会先建立
Action类的对象实例,但并不会调用Action方法 ,而是先将Action类的相应属性放
在ValueStack对象顶层节点(ValueStack对象相当于一个栈)
可以省写#号 如:<s:property value="user.username">
************************************
注:ValueStack需要使用服务器跳转才行
***************************************
前台:<input type="text" name="username">
<input type="text" name="password">

Test1Action:
private String username;
private String password;
private Student student;
getter/setter
execute method just return success

Test2Action
private Student student;
execute method just return success
服务器跳转:
<action name="test1" class="com.test.test1Action">
<result type="chain">
<param name="actionName">test2</param>
</result>
</action>
<action name="test2" class="com.test.test2Action">
服务器跳转 <result>
test2.jsp
</result>
</action>

test2.jsp
从值栈中取用户名:<s:property value="username"/>

此时值栈里有Test1Action,Test2Action的信息,且Test2在顶层,先会在Test2Action里取值,如果没有,再到Test1Action中取
如果想取值栈中第二个对象,则使用Top获取法:<s:property value="[1].top.student">

Top语法:取栈中的对象(也即Action) 从0开始
N语法获取属性:<s:property value="[1].student"> 从0开始
@语法:

栈:后进先出
======================================================
客户端跳转
<action name="test1" class="com.test.test1Action">
<result type="redirectAction">
<param name="actionName">test2</param>
</result>
</action>
<action name="test2" class="com.test.test2Action">
服务器跳转 <result>
test2.jsp
</result>
</action>


此时值栈里只有test2Action的信息了。

========================================================
<action name="test1" class="com.test.test1Action">
<result type="redirectAction">
<param name="actionName">test2?username=${username}</param>
</result>
</action>
<action name="test2" class="com.test.test2Action">
<result type="redirect">
test2.jsp
</result>
</action>

此时值栈里没有信息

在Action里也可以往ValueStack里手动的添加对象
ActionContext.getContext().getValueStack().push(new Student());

调用Action中的静态方法
<s:property value="@test1@get()"/>

获取ValueStack中第一个学生的信息
<s:property value="[0].username"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值