You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Shamdasani Nimmi-ANS004 <AN...@NAmerica.mot.com> on 2000/08/09 17:59:38 UTC

Hashtable keys() and elements() order [Was: Question about Select /Options tag]

Thanks Craig for the response.

Does anyone know whether or not the keys() and elements() methods of a
Hashtable enumerate things in the same order? 

I have tried to look into some Java books but haven't found the answer.

TIA.

-Nimmi
-----Original Message-----
From: Craig R. McClanahan [mailto:Craig.McClanahan@eng.sun.com]
Sent: Tuesday, August 08, 2000 10:44 PM
To: struts-user@jakarta.apache.org
Subject: Re: Question about Select/Options tag


Shamdasani Nimmi-ANS004 wrote:

> I want to use the 'Select' and 'Options' tags for presenting a combo-box
to
> the user. The bean 'SelectionBean' has 2 properties:
>
> *       selectedCommodity - This property should get set to the value
> selected by the user in the combo-box.
>
> *       commodityList - which is a Hashtable of (code, displayName).
> 'displayName' is what the user sees on the combo-box and 'code' is the
value
> of the item.
>
> I suppose this is what the select tag should be:
>
> <struts:select name='SelectionBean" property="selectedCommodity">
>

You do not need to specify the "name" attribute if this is the same bean you
named on the <struts:form> element.

>
> Now for the 'options' tag, do I need to extract commodityList.keys() and
> commodityList.elements() into String arrays to set the 'name' and
> 'labelName' respectively of the 'options' tag or is there a better way??
>
>

If you use a Hashtable as the collection directly, Struts will end up using
the
values (not the keys) for what it iterates through.  Thus, you will need to
set
up a collection or enumeration that refers to the keys and another for the
values.

One approach would be a scriptlet:

    <%
        pageContext.setAttribute("myKeys", commodityList.keys());
        pageContext.setAttribute("myValues", commodityList.elements());
    %>

and you can use "myKeys" and "myValues'" as the collections that drive the
<struts:options> tag.  This assumes that the keys() and elements() methods
of a
Hashtable enumerate things in the same order -- something I am not sure
about.

I will look at a way to support a case where if the collection you specify
is a
Map (or a Hashtable in JDK 1.1 compatible mode), it assumes that the keys
represent what you want returned as the field value, and the elements (when
converted to a String) represent the displayed options in the combo box.

> Thanks.
>
> -Nimmi

Craig


Re: Hashtable keys() and elements() order [Was: Question about Select /Options tag]

Posted by John Tangney <jt...@knowledgeplanet.com>.
My guess is that it's not specified, but you can always look at the code for
java.util.Hashtable and see. I wouldn't rely on it if I were you...

--johnt


> Does anyone know whether or not the keys() and elements() methods of a
> Hashtable enumerate things in the same order?