jQuery in Action, 2nd edition*
The moose likes JSP and the fly likes Map to Dropdown with JSTL & EL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Map to Dropdown with JSTL & EL" Watch "Map to Dropdown with JSTL & EL" New topic
Author

Map to Dropdown with JSTL & EL

Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
This seems like it should be an easy one, but it's evading me...

Say you have a class that is a HashMap:



And you want to populate a web page like this:


I'm trying to do this without scripting.

In my servlet I bind the CreditCards object to the request, and I can get to the values if I hard code them in the EL, but of course I don't want to do that.



It seems like this would be a really common thing to do, but, like I said, all of my trials have been unsuccessful.

So how is going from a map to a drop-down without scripting really supposed to be done?

--Dale--
[ October 17, 2004: Message edited by: Dale Seng ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

I would not bind the HashMap itself to the scoped variable, but rather the Set that results from the entrySet() method.

This will give you a collection that you can iterate over where each element is a Map.Entry instance and for which you can easily access the key and value properties via the EL.

Bear in mind that by using a HashMap, you are giving up the ability to order your dropdown options. I'd either use a TreeMap or a List of tuples.
[ October 17, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
Wow! Thanks for the nearly instant reply!

I actually went down the Set road, but couldn't figure out how to pull-out the keys values.

I suppose on the useBean I would put id="MY_SET" and class="java.util.Set". And on the <jsp:forEach ...> I suppose I'd put items=${MY_SET} and var="thing". So that would give me access to ${thing}, which I suppose would be the key. But I still don't know how to get to the value.

--Dale--
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

Firstly, name things according to conventions. So it would be "mySet".

Then do something like the following:



(assuming JSP 2.0).

Be sure to understand why the above works.
[ October 17, 2004: Message edited by: Bear Bibeault ]
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
Thanks. I think you have provided me with everything I need. For such instant service I must compliment you on your kindness and wisdom.

About using something orderable (TreeMap vs HashMap) point taken. I'll try that.

About the all caps thing, that came from the code I started with. Maybe I'll challenge the author.

Finally, I have not figured-out why it works (I haven't even got it working yet), but I'm looking into why it would work like that. My first thought would be maybe there's a getKey and getValue method somewhere.

--Dale--
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

Maybe I'll challenge the author.


Do. Not following standard conventions makes code surprisingly hard to read.

My first thought would be maybe there's a getKey and getValue method somewhere.


Hint: Map.Entry
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
Well, even with all of the hand-holding so far, I can not find the magic class and/or type to put on the <jsp:useBean...> line.

I've tried getting an entry set from a HashMap and from a TreeMap. I've done a getClass().getName() and tried whatever that said. I've tried various java.util classes and interfaces, and it's still evading me. No matter what I put, I get class cast exception at run time or invalid class or type during compile. Javadocs don't seem to help much because all they say is that you get back a java.util.Set from an entrySet() call, but that doesn't seem to work on the class= or type=.

I give up. What's the magic class and/or type to use on the jsp:useBean?

--Dale--
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

If you are not using Java scriplets on your page and you are relying on the servlet conteoller to place the Set onto the request as a scoped variable, why do need the useBean at all?
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
Originally posted by Bear Bibeault:
why do need the useBean at all?

I don't need it. I had it in there at first to see if I was getting the attribute at all (I had some text in the body that said 'it was null').

Once I took it out, it the page compiled and ran. Thanks again, Bear, for your assistance.

To recap:
I changed my CreditCards class to be a TreeMap. Then, instead of binding the class itself to the request, I bound the result of calling entrySet() on the class. I removed the <jsp:useBean...> in the jsp, since I didn't need it once the <c:forEach ...> was working. The result of calling entrySet() on a TreeMap, and then iterating with <c:forEach...> gives me items of type Map.Entry, which have methods getKey and getValue, and so can be used like ${item.key} and ${item.value}.

--Dale--
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

You got it!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

The important thing to remember is that adjusting the format of your data in your servlet controller to be EL-friendly is always a lot easier than trying to perform on-page contortions on "non-friendly" data.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Map to Dropdown with JSTL & EL