Struts 2 - Type conversion problems with Map and generics

In my action class I have a Map<Long, Set<String>> attribute, named accountsMap. Lets say the key values I get are 100,101,102. I have coded my checkbox in the JSP so that the POST parameters look like this:

accountsMap[100]=string1
accountsMap[100]=string2
accountsMap[101]=string3
accountsMap[101]=string4
accountsMap[102]=string5

So the resulting map should contain 3 entries with keys 100,101,102. The Map entry with key 100 should have a Set with 2 Strings (string1 and string2) as its value. Hope you get the idea.

I do get a populated Map in my action from struts on submitting the page. But the Map's generics information seems to have been discarded. Firstly, the keys of the Map are of type Integer and not Long as the original Map expects. Then, the Map's values are of type String[]; and not Set<String>. Struts is able to set these values using my setter, but when I do anything with the Map assuming key type as Long and value type as Set, I get class cast exceptions.

How do I get struts to set the correct type of values in the Map? Please help.

Answers


Solved!

I did not provide a getter for the attribute because it was write-only from the page - I wouldn't have a value for it when the page loads. I added a getter (as a desperate measure) and it solved the problem! Whodathunk?! :-)


Need Your Help

Having trouble getting Fogbugz API response into a SimpleXML object

simplexml fogbugz

I'm trying to write a wrapper around the fogbugz API, starting with getting a login token. I don's seem to be able to get the token into my wrapper object.

vector of string, each strings length and strlen in c++

c++ string vector

I have a vector of strings. I need to find the length of each string. Then I assume that when i convert each string to char array and do a strlen on the char array , the values should match.