struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Robert Edgar" <>
Subject RE: options tag
Date Fri, 30 Jun 2000 06:17:49 GMT
Just a thought but would it make sense to use a hashtable for this rather
than two arrays?


-----Original Message-----
From: Craig R. McClanahan []
Sent: Friday, June 30, 2000 1:27 PM
Subject: Re: options tag

Peter Cheung wrote:

>     I noticed that subscription.jsp in the example uses a "options" tag
> the code below):
>       <struts:select property="type">
>         <struts:options name="serverTypeValues"
>                    labelName="serverTypeLabels"/>
>       </struts:select>
>     After I read thru the tag.html, I am still not understand of how to
> provide the collection of the label and value to this tag, esp. I cannot
> locate the piece of code that creates the "serverTypeValues" or the
> "serverTypeLabels" objects. I wonder if anyone explain to me how those 2
> objects are being created. Thx in advance.

In the particular case of the example, they are being created (as string
in the scriptlet at the top of the page:

            new String[] { "imap", "pop3" });
            new String[] { "IMAP Protocol", "POP3 Protocol" });

Decomposing this a little, we see that the setAttribute() method takes two
parameters -- a name by which this object will be accessible, and an Object
which, in this case, is a String array.  The stuff in curly brackets is the
initialization of the (unnamed) arrays, which each end up with two elements.

In a real application, I expect that you would normally be pre-initializing
values and corresponding labels objects in the application's startup code,
probably storing them as servlet context attributes.  Alternatively, they
have been computed in an Action class and stored as request attributes prior
forwarding to this page (this might be needed so that you can get the
translations).  The important things to remember:

* The number of entries in the "labels" list has to match the
  number of entries in the "values" list.

* If you do not specify "labelName" or "labelProperty", the
  values list is used for both the value returned to the server
  and the value displayed to the user.

* There is no "scope" attribute on the <struts:options> tag,
  because the tag internally uses PageContext.findAttribute
  to search through all the scopes (page, request, session,
  and application) until it finds a bean with the name you

Hope this helps.


View raw message