简体   繁体   English

如何使用jsp中的键从Java映射中获取值,但是键是javascript值

[英]How to get value from a java map using key in a jsp, but key is a javascript value

I have a jsp page , In this page I get a hash map from request attribute and I want to extract value for a specific key . 我有一个jsp页面,在此页面中,我从请求属性获取哈希映射,并且我想提取特定键的值。 The key is availble on jsp . 该键在jsp上可用。 how can I extarct value from map using this key? 如何使用此键从地图上提取值?

I solved this problem by converting java map to java script associative array and then I could fetch value from that array. 我通过将Java映射转换为Java脚本关联数组来解决此问题,然后可以从该数组中获取值。 I would like to share the code : 我想分享代码:

<%
Map<String,String> currencyCodeMap = (Map<String,String>)application.getAttribute("currPrecisionCodeMap"); 
%>

<script language="javascript">


        var map = new Array();
        <%
        for (Map.Entry<String, String> entry : currencyCodeMap.entrySet()) {%>
                map['<%=entry.getKey()%>'] = '<%=entry.getValue()%>';
        <%}
        %>

var currencyCode = document.AccForm.currencyname.options[document.AccForm.currencyname.selectedIndex].text;

   alert(map[currencyCode ]);// gives value

</script>

above code is working fine but Can Someone provide better solution?? 上面的代码工作正常,但有人可以提供更好的解决方案吗?

If i understand your question right, you have init your hashmap in JSP using , than using JSTL u can iterate it. 如果我正确理解您的问题,则可以使用,在JSP中初始化哈希图,而不是使用JSTL可以对其进行迭代。

<jsp:useBean id="hm" type="java.util.Hashtable<java.lang.Long, java.util.ArrayList<java.lang.String>>" scope="request"/>

<c:forEach items="${hm[key]}" var="item">
    <c:out value="${item.value}" />
</c:forEach>

Also, you can do next: 另外,您可以执行以下操作:

pageContext.setAttribute("key", your_key);

And than using it in JSTL: 而不是在JSTL中使用它:

${your_key}

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

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