Dependent List box

Hello Friends,
I am new to Java and JSP. I am using Struts 2.0 for my development. I have an issue. I am developing a set of list boxes which are dependent on one another. For example, A country has many states and each state has many county’s and each county has many schools. Depending on the selection right from country–> state–> county–> school all are dependent. On selecting country, all its states populate in another list box… similarly upon selecting a state, all its corresponding county’s populate in another list box… and this goes on… Can anybody please give my a suggestion on how to do it. All the data is stored in database and I have to retrieve the data from DB. If you have the code or the logic, please feel free to share it by helping this newbie.
Thanks a lot for considering.

Lets say you load your page after your Struts Action sets an ArrayList (e.g. arrayListofAllCountries) on your ActionForm whichcontains a list of countries, each country in the list is just a String object.


Now your web page loads and you create your select list of countries like so.

<html:select property="country">
	<html:optionsCollection property="countries" />

Now, when they pick country, you want to fill another select list containing states. The simplest approach for a newbie is to add a javascript onchange event to the country SELECT box on the web page so that as you change the chosen entry in the select box, your country SELECT list onchange event re-submits the form back to the Struts Action.

<html:select property="country" onchange="document.forms[0].submit();">
	<html:optionsCollection property="countries" />

And when the web page loads, this turn in to the following HTML:

<SELECT name="country" onchange="document.forms[0].submit();">
	<option value="Albania">Albania</option>
	<option value="Albania">Botswana</option>	

The document.forms[0].submit(); assumes the form you want to submit is the first form on your web page.

Your javascript submit sends the form data back to the Struts Controller framework automatically will call the setCountry(“Botswana”) method on your ActionForm bean with the value of the selected option from the form.

Your Struts Action will now be given the ActionForm bean where you call getCountry() to know the selected country. You then have set on in the Struts ActionForm the ArrayList of the states (again a list of Strings for simplicity) based on the chosen country.


When your Struts Action forwards back to the JSP page, it reloads the web page building the
using the Struts

<html:select ... and <html:optionsCollection ...

Struts tags to create HTML select lists containing only the relevant options that the Struts Action bean for the states.

<html:select property="state" onchange="document.forms[0].submit();">
	<html:optionsCollection property="states" />

On your ActionForm you would also need to define a state property with getState and setState methods to hold the chosen state submitted by the form the next time around.

You continue along this approach with the counties and schools as well.

Thanks a tonn my friend… that was really helpfull… i will try this one out and let you know the outcome on this…
Thanks once again !!