关系图:
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: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
EL表达式取值:
EL:requestScope.list:
[0, 1, 2, 3, 4]
EL:list:
[0, 1, 2, 3, 4]
ognl:ContextMap list2: <br><s:property value="list2"/><br>
ognl:ContextMap #list2: <br><s:property value="#list2"/><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根元素的对象中取值加#号是正确显示值。