struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
Subject Re: options tag
Date Fri, 30 Jun 2000 05:27:11 GMT
Peter Cheung wrote:

>     I noticed that subscription.jsp in the example uses a "options" tag (see
> 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 arrays)
in the scriptlet at the top of the page:

    <%
        pageContext.setAttribute("serverTypeValues",
            new String[] { "imap", "pop3" });
        pageContext.setAttribute("serverTypeLabels",
            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 the
values and corresponding labels objects in the application's startup code,
probably storing them as servlet context attributes.  Alternatively, they could
have been computed in an Action class and stored as request attributes prior to
forwarding to this page (this might be needed so that you can get the correct
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
  specify.

Hope this helps.

Craig



Mime
View raw message