Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

myMap.get(myKey) with JSTL?

Thread view: 
Christine Mayer - 16 Oct 2007 11:06 GMT
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
Lew - 16 Oct 2007 14:14 GMT
> Hi, I need to get a map entry based on a key - how can this be done
> using the JSTL?
[quoted text clipped - 6 lines]
>
> but this seems pretty stupid to me.

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

Signature

Lew

Christine Mayer - 16 Oct 2007 14:42 GMT
This works:
    <c:out value="${myKey}"/> Output: E.g. 4711
    <c:out value="${myMap}"/> Output: The content of the entire object

This however does not work(empty output):
    <c:out value="${myMap['4711']}"/>
    <c:out value="${myMap[myKey]}"/>
    <c:out value="${myMap.myKey}"/>
    <c:out 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:out value="${row.myValue1}"/>
           <c:out value="${row.myValue2}"/>
       <c:out value="${row.myValue3}"/>
    </c:forEach>
Lew - 16 Oct 2007 14:52 GMT
> This works:
>     <c:out value="${myKey}"/> Output: E.g. 4711
[quoted text clipped - 10 lines]
> over. What I would like to achieve is something like:
>         <c:set value="${myMap[myKey]}" var="myBean"/>

Odd.  According to
<http://java.sun.com/javaee/5/docs/tutorial/doc/bnahq.html#bnahv>
> 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
<http://java.sun.com/javaee/5/docs/tutorial/doc/bnahq.html#bnain>
> ${departments[deptName]}
> The value of the entry named deptName in the departments map.

Signature

Lew

Daniel Pitts - 16 Oct 2007 20:35 GMT
> This works:
>         <c:out value="${myKey}"/> Output: E.g. 4711
[quoted text clipped - 15 lines]
>             <c:out 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.
Christine Mayer - 17 Oct 2007 13:54 GMT
Finnally, it worked out - it turned out that "A" != "a"!!!!!!! ;-)

Thx, Christine
Lew - 17 Oct 2007 15:01 GMT
> Finnally, it worked out - it turned out that "A" != "a"!!!!!!! ;-)

Was this something that turned up in a key comparison?

Signature

Lew

Daniel Pitts - 17 Oct 2007 15:22 GMT
> Finnally, it worked out - it turned out that "A" != "a"!!!!!!! ;-)
>
> Thx, Christine

Uh, yeah. That's always been true with Java :-).


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.