简体   繁体   English

解析Java Hashmap <String,String> 在JavaScript中

[英]parse java Hashmap<String,String> in JavaScript

I am using Hashmap<String, String> from the controller in the jsp. 我正在从jsp的控制器中使用Hashmap<String, String> I need to parse the Hashmap and get the value passing the Key. 我需要解析Hashmap并获取通过Key的值。 I am trying to get the value by passing the key using the get method of hashmap, but it is not working. 我试图通过使用hashmap的get方法传递键来获取值,但是它不起作用。

var key = "keyString";
var valueStr = <%=hashMap.get(key)%>;

The above code is giving the error, key cannot be resolved to a variable. 上面的代码给出了错误,密钥无法解析为变量。 But when I am hardcoding the key value in the hashmap I am able to get the value. 但是,当我在哈希图中对键值进行硬编码时,我就能得到该值。

var valueStr = <%=hashMap.get("keyString")%>;

You're trying to reference key in the context of the JSP scriptlet, which is compiled on the server side. 您正在尝试在服务器端编译的JSP scriptlet上下文中引用key Not only is it in a different context entirely, but your JavaScript is handled on the client side, after the JSP scriptlet (compiled by the server) is already resolved. 它不仅完全在不同的上下文中,而且在解决了JSP scriptlet(由服务器编译)之后,您的JavaScript也在客户端处理。

In short, key isn't a variable until after the scriptlet is already resolved. 简而言之,直到脚本被解析后, key才是变量。

JSP is compiled at server side and Simple HTML is returned to client back. JSP在服务器端进行编译,并且将简单HTML返回给客户端。 You are expecting the client side variable (In browser's JVM) should be used for JSP Scriplet evaluation. 您期望将客户端变量(在浏览器的JVM中)用于JSP Scriplet评估。 Then you should pass it along with request to server. 然后,您应该将其与请求一起传递给服务器。

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

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