ATG Pune
ATG Pune

Reputation: 13

Compare map keys with some element and based on the result show its values

I need to retrieve values from a map of type Map <String, String> in jsp based on a condition. The condition is to compare map key with variable and if the key equals the variable show the value pertaining to that key. Here's what I am doing:

<c:if test="${ myMap.key eq myVariable }">
<jsp:getvalueof var="testVariable" value = "${ myMap.value }" />
</c:if>

What I am expecting to get is if the myMap.key equals myVariable, I should get the value pertaining to that key in "test" variable.

But this thing is not working. Please any idea anyone?

Thanks in advance :)

Upvotes: 1

Views: 1628

Answers (3)

Dimitri
Dimitri

Reputation: 8280

Since, you want to retrieve values from the map based on a condition, you can use the ternary operator instead. Try this :

<c:set var="testVariable" value='${ myMap.key eq myVariable ? myMap[myVariable] : "defaultValue" }'/>

Upvotes: 0

Francisco Paulo
Francisco Paulo

Reputation: 6322

You can directly access the map and get the value into a 'test' variable:

<c:set var="test" value="${myMap[myVariable]}"/>

Upvotes: 3

Dinoop paloli
Dinoop paloli

Reputation: 633

//use like this in jsp
<%
String val;
for(String key : myMap.keyset()){
    if(key.equals(myVariable )){
        val = myMap.get(key);
    }
}
%>
//on js use like this
var test = '<%=val%>';

Upvotes: 0

Related Questions