Over a million developers have joined DZone.

Struts 2 : Creating and Accessing Maps

DZone's Guide to

Struts 2 : Creating and Accessing Maps

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

Today, in this post, I am going to discuss how to create and access HashMaps in Struts 2.

My environment has the following jar files.


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>

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>

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]}">

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">

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]}">

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

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}