You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by sriram <sr...@nunet-tech.com> on 2003/07/01 11:17:33 UTC

Populating select boxes dynamically

Hi,
 
I am fairly new to struts. I am using struts 1.0. I have couple of list boxes in my input form which need to populated with data retrieved from the database.
 
What is the process to be followed? Can somebody give me links to related examples on the net?
 
Any information would be very helpful to me.
 
Thks
Sriram

RE: Populating select boxes dynamically

Posted by Alex Shneyderman <al...@law.columbia.edu>.
> What is the process to be followed? Can somebody give me links to
related
> examples on the net?

There are many different ways. From do it yourself to get your options
>From a collection that is a bean in one of the contexts. 

I will explain do it yourself method the others are similar with an
exception there are better ways to point to your collections.

Suppose you have a SystemDictionary object that has a bunch of static
listThisAndThat () methods that return collections. Now to turn this
Collection into options drop-down. 
You do the following in your JSP:
<% page.setAttribute ("myCountries", SystemDictionary.listCountries ());
%>
:
:
<html:select name="country">
  <html:options collection="myCountries" property="countryId"
labelProperty="countryName" />
</html:select>

This will render a drop-down on your HTML that will have countries to 
Select from. Note Your SystemDictionary.listCountries () should return
A collection of Country objects that have at least two methods
getCountryId () and getCountryName (). Each of which will be called to
render your options as follows:
 <option value="<%= currentCountry.getCountryId ().toString () %>"><%=
currentCountry.getCountryName ().toString () %></option>

> Any information would be very helpful to me.

Just read <html:select>, <html:options> and <html:optionsCollection>
documentation. It is a bit confusing at first but if you take your time
to read it thru and play around with it, you going to save some time.

Alex.



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org