Create Map dynamically using OGNL

Create Map dynamically using OGNL

Java Code
Map<Integer,String> myMap =  new HashMap<Integer,String>();
myMap.put(1,"one");
myMap.put(2,"two");
myMap.put(3,"three");
myMap.put(4,"four");
myMap.isEmpty() : myMap.isEmpty

OGNL Expression
#{1:"one", 2:"two", 3:"three", 4:"four"}

Example:

1)
<s:iterator value="#{1:'one', 2:'two', 3:'three', 4: 'four'}" status="counter">
<s:property value="#counter.index"></s:property>
<s:property value="key"></s:property>
<s:property value="value"></s:property>
</s:iterator>
Here we are creating the map dynamically in the value attribute.
In this case # sign is used to creat the map object.

2)
<s:iterator value="#request.studentsMap" status="counter">
<s:property value="#counter.index"></s:property>
<s:property value="key"></s:property>
<s:property value="value"></s:property>
</s:iterator>
Here we are taking the map named 'studentsMap' from the request scope.
here # sign is used to take the request object reference from the ActionContext.

3)
<s:set value="1" var"studentNumber"/>
<s:property value="#request.studentsMap[#studentNumber]"/>