简体   繁体   English

如何在jsp中的Javascript中访问映射键,值(java.util.Map)?

[英]How to acces a map key,value (java.util.Map) in Javascript within jsp?

Problem is I'm not able to get the map key and its values (which is again a Java ArrayList) in Javascript function 问题是我无法在Javascript函数中获取map键及其值(再次是Java ArrayList)

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: range是一个Java映射,如下所示:

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? 您能告诉我如何在上述Javascript函数中显示map键及其相关的arraylist吗?

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. 您将需要迭代Java代码来创建将填充地图的JS代码。

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. 认为JS和Java在完全独立的VM(很可能在不同的计算机)中都可以工作,因此您不能只传递对Java的引用并期望它在JS中“起作用”。 You have to copy all the data. 您必须复制所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM