根据 JSTL 的键从 hashmap 中获取值 [英] Get value from hashmap based on key to JSTL

查看:26
本文介绍了根据 JSTL 的键从 hashmap 中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据key获取HashMap的值.

I want to get the value of HashMap based on key.

HashMap<String, ArrayList<String>> map 
    = new HashMap<String, ArrayList<String>>();
ArrayList<String> arrayList = new ArrayList<String>();

map.put("key", arrayList);
request.setAttribute("key", map);

我所做的是

<c:forEach var="map" items="${requestScope.key}">
    <c:forEach var="hash" items="${map.value}">
        <option><c:out value="${hash}"/></option>
    </c:forEach>
</c:forEach>

但它似乎正在打印所有内容,我想要做的是获取取决于键的值,例如:hash.key 或其他内容

But it seems it's printing everything, what i want to do is to get the value depends on key like: hash.key or something

更新:
我做了类似的事情,但它仍然不起作用

UPDATE:
I did something like this but it still doesn't work

<c:forEach var="map" items="${requestScope.key}">
    <c:forEach var="hash" items="${map['key']}">
        <option><c:out value="${hash}"/></option>
    </c:forEach>
</c:forEach>

和 StackTrace:在 java.util.HashMap$Entry 类型上找不到属性外部"
我很确定真的有那种钥匙.

and the StackTrace: Property 'External' not found on type java.util.HashMap$Entry
I'm pretty sure that there is really that kind of key.

推荐答案

如果您所做的只是获取地图中单个条目的值,则根本不需要遍历任何集合.稍微简化 gautum 的响应,您可以获得命名映射条目的值,如下所示:

if all you're trying to do is get the value of a single entry in a map, there's no need to loop over any collection at all. simplifying gautum's response slightly, you can get the value of a named map entry as follows:

<c:out value="${map['key']}"/>

其中map"是集合,key"是您尝试为其提取值的字符串键.

where 'map' is the collection and 'key' is the string key for which you're trying to extract the value.

这篇关于根据 JSTL 的键从 hashmap 中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆