Hi David,

When starting out, it helps to keep in mind that for the most part whenever
you see any custom tag attribute called "property", the tag is actually
calling a method on some object to retrieve a private member variable from
that object (via JavaBeans introspection).  For instance, the <html:select>
tag in my original example is calling getHomeDepartmentRoles() on the form
bean (the default bean in this case) to get the value of
homeDepartmentRoles.

First of all, homeDepartmentRoles in my example is a String array, not a
String.  This can probably be a String if your select box doesn't allow for
select multiple (i.e. it's a drop-down versus a list box), although I
haven't worked with that case yet.  It has to be an array in my example
because it contains multiple elements - each array element represents one
value that will be selected when the select box is displayed.
homeDepartmentRoles is a member variable of the ActionForm, so there is a
getHomeDepartmentRoles() method on my ActionForm that returns a String[]
array (you should also have a setHomeDepartmentRoles(String[]) method for
when the form is submitted).  Before the form is displayed, the way you can
get data into the homeDepartmentRoles member variable is by populating it in
the form bean from your Action class.  So in your Action class you would
have whatever code you need to create the list of selected roles, then store
that on the form via form.setHomeDepartmentRoles(roles), and then when the
form is displayed the <html:select> tag will call getHomeDepartmentRoles()
to get the String[] of selected values.

The <html:options> tag takes a java Collection object in the "collection"
attribute.  It doesn't have to separate the values in any special way - it
just iterates through the Collection of LabelValueBean objects, with each
item in the Collection representing a different item in the list.  The
"property" and "labelProperty" attributes tell the <html:options> tag which
methods to call on the LabelValueBean object to create the value and text of
each HTML <option> tag.  In my original example, the <html:options> tag
would call the methods getLabel() and getValue() methods on each
LabelValueBean object in the Collection to display the value and text.  This
will create all the options in your select box.

Here's an example...

Let's say that getHomeDepartmentRoles() returns me a String[] with the
following 3 items: 5, 6, 8

Let's also say that the roles Collection looks like this:
[new LabelValueBean("5", "Role 5")],
[new LabelValueBean("6", "Role 6")],
[new LabelValueBean("7", "Role 7")],
[new LabelValueBean("8", "Role 8")],
[new LabelValueBean("9", "Role 9")]

The HTML that will be generated, based on my original example, is this:

<select name="homeDepartmentRoles" multiple="multiple">
    <option value="5" selected="selected">Role 5</option>
    <option value="6" selected="selected">Role 6</option>
    <option value="7">Role 7</option>
    <option value="8" selected="selected">Role 8</option>
    <option value="9">Role 9</option>
</select>

You can see that when an item in the homeDepartmentRoles array matches up
with an item in the roles Collection, it is displayed as selected.

Hopefully this clears things up.  If not, reply to the list and cc to me and
hopefully myself or somebody else can try to clear up any specific
questions.

Justin


----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, December 12, 2002 10:42 AM
Subject: Re: dropdowns being reset


>
> Hi Justin,
>
> Thanks for your reply, I'm very new to struts. Where do you declare the
> homeDepartmentRoles string? and how does the <html:option tag know how to
> separate out the values? are they delimited in any way?
>
> Thanks,
> David
>
>
>
>
>               "Justin Ashworth"

>               <[EMAIL PROTECTED]         To:      "Struts Users
Mailing List" <[EMAIL PROTECTED]>
>               rg>                               cc:
>                                                 Subject: Re: dropdowns
being reset
>               12/12/2002 15:21
>               Please respond to "Struts
>               Users Mailing List"
>
>
>
>
>
> Hi David,
>
> Here is an example.  In this example, homeDepartmentRoles is a String[],
> representing all of the roles that a particular user has.  The "roles"
> collection in the <html:options> tag is a Collection of LabelValueBean
> objects (see
>
http://jakarta.apache.org/struts/api/org/apache/struts/util/LabelValueBean.h
>
> tml).  The LabelValueBean objects in the collection represent ALL items in
> the select list.  To put it another way, the collection specified in the
> <html:options> tag should have ALL items you want to display in your
select
> box, whereas the <html:select> "property" attribute is the array that
> stores
> all items which will show up selected when the select box is displayed.
>
> <html:select property="homeDepartmentRoles" multiple="true">
>     <html:options collection="roles" property="label"
> labelProperty="value"/>
> </html:select>
>
> This can be done a number of different ways, but this is the best way that
> I
> have found works for my needs.
>
> HTH,
>
> Justin
>
> ----- Original Message -----
> From: <[EMAIL PROTECTED]>
> To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
> Sent: Thursday, December 12, 2002 8:07 AM
> Subject: RE: dropdowns being reset
>
>
> >
> > Is there any way you could mail me sample code for this Options tag?
> >
> > Thanks,
> > David
> >
> >
> >
> >               shirishchandra.sakhare@ubs.
> >               com                                 To:
> [EMAIL PROTECTED]
> >                                                   cc:
> >               12/12/2002 12:33                    Subject: RE: dropdowns
> being reset
> >               Please respond to "Struts
> >               Users Mailing List"
> >
> >
> >
> >
> >
> > try using html:options instead..and supply it with the collection or
> array
> > of
> > indexes...
> >
> > because if in html:select u specify property attribute, and the property
> is
> >
> > there in the request(which should be the case when u forward the request
> > back
> > to same jsp with errors..),the same is used to preselect a element.
> > I think may be as u are using html:ootion in a loop, u are having this
> > problem.
> >
> > regards,
> > Shirish
> >
> > -----Original Message-----
> > From: david.heagney [mailto:[EMAIL PROTECTED]]
> > Sent: Thursday, December 12, 2002 1:17 PM
> > To: struts-user; david.heagney
> > Subject: dropdowns being reset
> >
> >
> > Hi,
> >
> > I have a page in my STRUTS app which dynamically populates a dropdown
> list:
> >
> > Month
> >
> > <html:select property="fromMonth">
> > <option value="--">--
> > <% for (int i=1; i <= 12; i++) { %>
> >       <option value="<%= i %>"><%= getMonthName (i) %>
> > <% } %>
> > </html:select>
> >
> > getMonthName() returns the month as a string based in the passed
integer,
> 1
> > - 12.
> >
> > If the user submits the form and leaves out a required field, the above
> > dropdown gets re-set, i.e. doesn't retain the selection when the page is
> > refreshed with it's errors... does anyone know how i can avoid this?
> >
> > Thanks,
> > David
> >
> >
> > This message is for the designated recipient only and may contain
> > privileged, proprietary, or otherwise private information.  If you have
> > received it in error, please notify the sender immediately and delete
the
> > original.  Any other use of the email by you is prohibited.
> >
> >
> > --
> > To unsubscribe, e-mail:   <
> > mailto:[EMAIL PROTECTED]>
> > For additional commands, e-mail: <
> > mailto:[EMAIL PROTECTED]>
> >
> >
> >
> > --
> > To unsubscribe, e-mail:   <
> > mailto:[EMAIL PROTECTED]>
> > For additional commands, e-mail: <
> > mailto:[EMAIL PROTECTED]>
> >
> >
> >
> >
> > This message is for the designated recipient only and may contain
> > privileged, proprietary, or otherwise private information.  If you have
> > received it in error, please notify the sender immediately and delete
the
> > original.  Any other use of the email by you is prohibited.
> >
> >
> > --
> > To unsubscribe, e-mail:
> <mailto:[EMAIL PROTECTED]>
> > For additional commands, e-mail:
> <mailto:[EMAIL PROTECTED]>
> >
>
>
> --
> To unsubscribe, e-mail:   <
> mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <
> mailto:[EMAIL PROTECTED]>
>
>
>
>
> This message is for the designated recipient only and may contain
> privileged, proprietary, or otherwise private information.  If you have
> received it in error, please notify the sender immediately and delete the
> original.  Any other use of the email by you is prohibited.
>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to