Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

java - Get value from hashmap based on key to JSTL

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>

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>

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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']}"/>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...