Struts2中ActionContext、Ognlcontext、OgnlValueStack对象关系图,ognl表达式根元素分析

10 篇文章 0 订阅
2 篇文章 0 订阅

关系图:




Action:

public String execute() throws Exception{

List<Integer> list = new ArrayList<Integer>();
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();

for (int i = 0; i < 5; i++) {
list.add(i);
list1.add(i+10);
list2.add(i+20);
}


ActionContext ac = ActionContext.getContext();

// 获取值栈对象:
ValueStack vs = ac.getValueStack();

// 获取contextMap对象
Map<String, Object> contextMap = ac.getContextMap();

vs.set("list", list);
contextMap.put("list", list1);
contextMap.put("list2", list2);

return "success";

}

jsp:

    ognl表达式取值:   <br/>
    CompoundRoot:<br/>
    ognl:CompoundRoot list: <br><s:property value="list"/><br>
    ognl:CompoundRoot list【1】: <br><s:property value="list[1]"/><br>
    ContextMap:<br/>
    ognl:ContextMap #list: <br><s:property value="#list"/><br>
    ognl:ContextMap #list【1】: <br><s:property value="#list[1]"/><br>
    
    EL表达式取值:<br/>
    EL:requestScope.list:<br> ${requestScope.list }<br>
    EL:list:<br> ${list }<br>


结果:

ognl表达式取值: 
ognl:CompoundRoot list: 
[0, 1, 2, 3, 4]
ognl:CompoundRoot list【1】: 
1

ognl:ContextMap #list: 
[10, 11, 12, 13, 14]
ognl:ContextMap #list【1】: 
11

结论:
在ContextMap下存储根元素的key值与CompoundRoot下list对象栈存储的key值一样时,获取ContextMap下根元素值必须加上#号,不然只能获取CompoundRoot下list对象栈下的相对应的值。所以平时用Ognl表达式,除了调用CompoundRoot下list对象栈外,其他都加上#号为妙。


EL表达式取值:
EL:requestScope.list:
[0, 1, 2, 3, 4]
EL:list:
[0, 1, 2, 3, 4]                      

结论:
在ContextMap下存储根元素的key值与CompoundRoot下list对象栈存储的key值一样时,EL表达式只会获取CompoundRoot下list对象栈存储的key值相对应的值;指定requestScope域与一样。使用struts2时,如果用EL表达式要注意重名问题。


----------------------------------------------------------------------------------------------------------------------------

jsp:
    ognl表达式调用ContextMap根元素的对象:<br/>
    ognl:ContextMap list2: <br><s:property value="list2"/><br>
    ognl:ContextMap #list2: <br><s:property value="#list2"/><br>    

    ognl表达式从ContextMap根元素的对象中取值:<br/>
    ognl:ContextMap list2【1】: <br><s:property value="list2[1]"/><br>
    ognl:ContextMap #list2【1】: <br><s:property value="#list2[1]"/><br>

结果:

ognl表达式调用ContextMap根元素的对象:
ognl:ContextMap list2: 
[20, 21, 22, 23, 24]                       
ognl:ContextMap #list2: 
[20, 21, 22, 23, 24]

ognl表达式从ContextMap根元素的对象中取值:
ognl:ContextMap list2【1】: 

ognl:ContextMap #list2【1】: 
21                                                  


结论:

ContextMap根元素的对象加不加#号值是一样的。

从ContextMap根元素的对象中取值不加#号取不到值的。

从ContextMap根元素的对象中取值加#号正确显示值


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值