myMap.get(myKey) with JSTL?

C

Christine Mayer

Hi, I need to get a map entry based on a key - how can this be done
using the JSTL?

I know I could say:
<c:forEach items="${myMap }" var="entry" >
<c:if test="${entry.key== myKey }">
</c:if>
</c:forEach>

but this seems pretty stupid to me.

Any idea how to solve this?

Thanks in advance,

Christine
 
L

Lew

Christine said:
Hi, I need to get a map entry based on a key - how can this be done
using the JSTL?

I know I could say:
<c:forEach items="${myMap }" var="entry" >
<c:if test="${entry.key== myKey }">
</c:if>
</c:forEach>

but this seems pretty stupid to me.

"${ myMap [myKey] }"
or
"${ myMap.myKey }"
 
C

Christine Mayer

This works:
<c:eek:ut value="${myKey}"/> Output: E.g. 4711
<c:eek:ut value="${myMap}"/> Output: The content of the entire object

This however does not work(empty output):
<c:eek:ut value="${myMap['4711']}"/>
<c:eek:ut value="${myMap[myKey]}"/>
<c:eek:ut value="${myMap.myKey}"/>
<c:eek:ut value="${myMap.'4711'}"/>

myMap contains a String key and a bean as value. The bean encapsulates
a list (that contains other beans again), that I later want to iterate
over. What I would like to achieve is something like:
<c:set value="${myMap[myKey]}" var="myBean"/>
<c:forEach items="${myBean.myList}" var="row">
<c:eek:ut value="${row.myValue1}"/>
<c:eek:ut value="${row.myValue2}"/>
<c:eek:ut value="${row.myValue3}"/>
</c:forEach>
 
L

Lew

Christine said:
This works:
<c:eek:ut value="${myKey}"/> Output: E.g. 4711
<c:eek:ut value="${myMap}"/> Output: The content of the entire object

This however does not work(empty output):
<c:eek:ut value="${myMap['4711']}"/>
<c:eek:ut value="${myMap[myKey]}"/>
<c:eek:ut value="${myMap.myKey}"/>
<c:eek:ut value="${myMap.'4711'}"/>

myMap contains a String key and a bean as value. The bean encapsulates
a list (that contains other beans again), that I later want to iterate
over. What I would like to achieve is something like:
<c:set value="${myMap[myKey]}" var="myBean"/>

Odd. According to
To refer to properties of a bean or an Enum instance, items of a
collection, or attributes of an implicit object,
you use the . or [] notation, which is similar to the notation used by ECMAScript. ....
In contrast, an item in a Map can be accessed using a string literal key; no coercion is required:

${customer.orders["socks"]}
.... and
${departments[deptName]}
The value of the entry named deptName in the departments map.
 
D

Daniel Pitts

This works:
<c:eek:ut value="${myKey}"/> Output: E.g. 4711
<c:eek:ut value="${myMap}"/> Output: The content of the entire object

This however does not work(empty output):
<c:eek:ut value="${myMap['4711']}"/>
<c:eek:ut value="${myMap[myKey]}"/>
<c:eek:ut value="${myMap.myKey}"/>
<c:eek:ut value="${myMap.'4711'}"/>

myMap contains a String key and a bean as value. The bean encapsulates
a list (that contains other beans again), that I later want to iterate
over. What I would like to achieve is something like:
<c:set value="${myMap[myKey]}" var="myBean"/>
<c:forEach items="${myBean.myList}" var="row">
<c:eek:ut value="${row.myValue1}"/>
<c:eek:ut value="${row.myValue2}"/>
<c:eek:ut value="${row.myValue3}"/>
</c:forEach>

what about ${myMap[4711]}?
If it is a String key, make sure that myKey is also a String.
Where does myKey come from? Might it be better to have whatever
constructs your model pull the value out of the map?

It should work the way you've tried, unless there is something else
going on.
 
C

Christine Mayer

Finnally, it worked out - it turned out that "A" != "a"!!!!!!! ;-)

Thx, Christine
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top