You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Mauricio T. Ferraz" <ma...@cilix.com.br> on 2003/12/10 18:38:18 UTC

Drow-Down list HashMap???

How can I generate I dropDown List with a HashMap getting the key(HashMap) how value(HTML) and the value(HashMap) how name(HTML)

With this HashMap:

    HashMap map = new HashMap();
    map.put("1","test1");
    map.put("2","test2");
    map.put("3","test3");
    map.put("4","test4");

And I want this output

<select name="teste">
<option value="1">teste1</option>
<option value="2">teste2</option>
<option value="3">teste3</option>
<option value="4">teste4</option>
</select>

Re: Drow-Down list HashMap???

Posted by Joe Germuska <jo...@germuska.com>.
See the documentation for the <html:optionsCollection> tag:  
http://jakarta.apache.org/struts/userGuide/struts- 
html.html#optionsCollection

Basically, your value for the "label" attribute would be "value" (the  
'property' of a Map.Entry element in each iteration through the Map)  
and your value for the ""value" attribute would be "key"

The only other problem is that java.util.HashMap is not ordered, so you  
can't rely on getting the entrySet in the order in which you put in the  
elements.  In Java 1.4 you can use java.util.LinkedHashMap, or if you  
need compatibility with earlier versions of Java, you can use  
commons-collections SequencedHashMap  
(http://jakarta.apache.org/commons/collections/api/org/apache/commons/ 
collections/SequencedHashMap.html)

Joe


On Dec 10, 2003, at 11:38 AM, Mauricio T. Ferraz wrote:

> How can I generate I dropDown List with a HashMap getting the  
> key(HashMap) how value(HTML) and the value(HashMap) how name(HTML)
>
> With this HashMap:
>
>     HashMap map = new HashMap();
>     map.put("1","test1");
>     map.put("2","test2");
>     map.put("3","test3");
>     map.put("4","test4");
>
> And I want this output
>
> <select name="teste">
> <option value="1">teste1</option>
> <option value="2">teste2</option>
> <option value="3">teste3</option>
> <option value="4">teste4</option>
> </select>
>
--
Joe Germuska
Joe@Germuska.com
http://blog.germuska.com
  "We want beef in dessert if we can get it there."
   -- Betty Hogan, Director of New Product Development, National  
Cattlemen's Beef Association