0

Problem is I'm not able to get the map key and its values (which is again a Java ArrayList) in Javascript function

My code is as below:

function myset() {
    var mymap="${range}";//range is a java map which is accessible here

// Here I need to display the mymap key and its value (which is an arraylist)
}

<select id="bn" name="bn" onchange="myset()">
</select>

range is a Java map as follows:

List<String> l=new ArrayList<String>();
l.add("123");
l.add("456");
l.add("789");
List<String> ls=new ArrayList<String>();
l.add("222");
l.add("456");
l.add("333");
Map<String,List<String>> m=new HashMap<String,List<String>>();
m.put("123", l);
m.put("456", ls);

Can you please tell me how to display the map key and its concerned arraylist in the above Javascript function?

2
  • 1
    What have you tried? What does it display when you do console.log(mymap);? Commented Jan 3, 2013 at 16:37
  • In fact, i have to populate the arraylist values in other dropdown list(second). For example if map key == 123 then i have to get its concerned arraylist to be populated in the other dropdown Other drop down is as below <select id="second" name="second"> </select> Can you please let me know how to do this? Commented Jan 3, 2013 at 16:52

1 Answer 1

1
var mymap="${range}";//range is a java map which is accessible here

Doing this, you are just doing

var mymap="#{range.toString()}" 

(or, if you have defined a converter, the value returned by it).

You will need to iterate in your Java code to create the JS code that will populate the map.

Think that JS and Java work in both completely separated VM (most likely in different computers), so you can not just pass a reference to Java and expect it to "work" in JS. You have to copy all the data.

Sign up to request clarification or add additional context in comments.

2 Comments

Very thanks for the reply.But it is displayed the range in alert(mymap) Output: mymap{123=[123, 456, 789, 222, 456, 333], 456=[]} Atlease let me know how to access the java map key and its values in a javascript function.i have this code in a jsp file. Please let me know the way.
If there is any other way please tell me

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.