2

I want to create a javascript map from a java map to set a dropdown value depending upon the value selected in another dropdown. Below is the code(not working):

var categoryAndReportsMap = new Object();
<% 
Map<String,Set<String>> categoryAndReportsJ = (Map<String,Set<String>>)      request.getAttribute("categoryAndReports");
for(Map.Entry<String,Set<String>> e : categoryAndReportsJ.entrySet()){ %>
categoryAndReportsMap[ <% e.getKey(); %> ] = <% e.getValue(); %>;
<% } %>

Please suggest how can I achieve this.

4
  • 4
    Use a JSON serializer. Commented May 6, 2013 at 14:22
  • @dystroy: the : vs = is absolutely not the problem here. The OP just neglected to look at JSP-generated output in webbrowser in order to see the trivial mistake. All those Java variables are namely been printed as JS variable names instead of as JS strings. Commented May 6, 2013 at 14:24
  • I voted up SLaks comment, but If you are interested and to give some information to try to understand how it works, as long as I know from javascript, maps doesn't exist. Instead you have associative arrays that works similar. Key => value. Eg. var asocArray = new Array(); asocArray["a"] = "a";... Commented May 6, 2013 at 14:26
  • Actually, JavaScript has neither maps nor associative arrays. When you assign string-keyed values to an Array, you're actually creating named properties on the array object itself, the same as if you were doing it on a new Object() instead of a new Array(). This becomes important when you try to iterate the values back out; using for...in syntax will net you all of the custom properties as well as "count", etc. Commented May 6, 2013 at 14:28

1 Answer 1

3

You need quotes around the keys and values :

categoryAndReportsMap["<%= e.getKey() %>"] = "<%= e.getValue() %>";

But this supposes those strings don't contain quotes themselves. The best solution would be to use a JSON serializer like the excellent gson, this would be as simple as

var categoryAndReportsMap = <%= gson.toJson(categoryAndReportsJ) %>;
Sign up to request clarification or add additional context in comments.

3 Comments

Note that this construct would still fail when the Java variable contains a doublequote or even just a newline or backslash. That's exactly why a JSON serializer is strongly recommended as a decent one already takes this into account.
@dystroy Thanks for the solution....Can you please suggest how to populate the values using categoryAndReportsMap in a dropdown. I tried document.getElementById('selected_report').value = categoryAndReportsMap[reportCategory]; but its not working. reportCategory is the key
to populate the values we need to create an array in javascript and populating the array by parsing the java object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.