从基于JSTL密钥的hashmap获取值 [英] Get value from hashmap based on key to JSTL
问题描述
我想根据键获取 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);
我做的是
What i did is
<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中找不到Property'External' .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的响应,你可以得到一个有名的map条目的值如下:
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屋!