Struts 2 : Creating and Accessing Maps
Join the DZone community and get the full member experience.
Join For FreeToday, in this post, I am going to discuss how to create and access HashMaps in Struts 2.
My environment has the following jar files.
struts2-core-2.1.8.1.jar
ognl-2.7.3.jar
Struts 2 makes extensive use of OGNL in order to retrieve the values of elements. OGNL stands for Object Graph Navigation Language. As the name suggests, OGNL is used to navigate an object graph. In this post, i am going to use the OGNL syntax to create Map on a jsp page, and show you how to iterate over it to fetch the keys and values from the map.
In the following example, i will create a map, on they fly in the iterator tag, and will use it in the body of the iterator tag.
<s:iterator value="#{'key1':'value1', 'key2':'value2'}" status="status">
<s:property value="#status.index"></s:property>
<s:property value="key"></s:property>
<s:property value="value"></s:property>
</s:iterator>
Note the syntax that has been used to create a Map on a jsp page in struts 2 using OGNL. Once the map is created, the iterator tag can be used to iterate over each element of the map.
Now suppose the map that we want to access is in a map inside the HttpRequest. Assume that some action somewhere in the chain has kept a map in the request using the key "myMap". In order to iterate over the elements of the map, we can do the following
<s:iterator status="status" value="%{#request.myMap}">
<s:property value="#status.index"></s:property>
<s:property value="key"></s:property>
<s:property value="value"></s:property>
</s:iterator>
Okay now, enough with iterating over maps. I don't think there are any more permutations for accessing maps. But if i do find more, ill document them down here.
Consider a case where you dont want to iterate over the entire map. Instead, all that you want to do is to extract a value form the map based upon a key that is already known to you in your jsp page.
Assume that you have a variable in page scope called "runtimeKey" that you set using the s:set tag. The value of this variable is a string key that can be used to get a value from a map.
Here is how you can fetch the value from the map without iterating over it.
<s:set value="'key_1'" var="runtimeKey"></s:set>
<s:property value="%{#request.myMap[#runtimeKey]}">
</s:property>
As you see, since the variable "runtimeKey" is an OGNL variable and is available on the value stack, it can be referenced using the # notations. Also not that instead of using the dot notation, I have used square brackets to fetch the key. This is because the value of my expression "#runtimeKey will only be evaluated when its inside the brackets. Also note that the value of the key runtimeKey is contained within single quotes to direct OGNL to evaluate it as a string when setting as the value for my key.
Consider another situation where your keys follow a pattern. For example key_1, key_2. And you have the values 1 and 2 as page scoped variables. So, now instead of having the string value of the key directly, you may have to construct the key using concatenation.
<s:set value="'1'" var="keyPattern">
</s:set>
Your key pattern was set above. And you Map has a key called key_1. So, here is how you would have to concatenate your strings in order to construct the key and fetch the value.
<s:property value="%{#request.myMap['key_'+#keyPattern]}">
</s:property>
Huh. So easy!
Thats all for now folks.. Stay tuned for more!
Happy Programming :)
From http://mycodefixes.blogspot.com/2010/11/struts-2-creating-and-accessing-maps.html
Opinions expressed by DZone contributors are their own.
Comments