Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Drop Down Lists.

 
Mike Troxclaire
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Gurus,

I am trying to build a JSP page with two drop down lists. The values in the second drop down list should be loaded from the database based on the value selected in the first drop down list. I have two tables 1st one is Customers and in that I have fields : ItemId and CustomerId, my second table is Items and in that I have fields : ItemId and Itemname. So, in my first drop down list I need the values from ItemId in the Customers table to be loaded when the user first accesses the page and when an ItemId is selected in the first drop down list it should pull the Itemname(s) from the Items table and populate the second drop down list. Hope that makes sense. Any help in this matter would be much appreciated.

Cheers,

Mike.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has been discussed many times in this and the HTML forum. Poke around a bit and you will find lots of good ideas.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
I've just added a demo project that shows how to do this using an MVC pattern.
It's packaged as a war file so you can just drop it into a running instance of Tomcat and test it.

http://simple.souther.us/not-so-simple.html
 
Sri Basavanahally
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,

You use two different forms to do this. I have a case where the drop downs in question are part of one big form. How do I handle such a case ?

Thanks for your help !

-Sri
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both select lists are in the same form.
 
Sri Basavanahally
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben ! I guess it works the same, then.

Now, you do some logic in the back-end to keep the drop down item the user selected to be selected when the page comes back.

So, if I have other form input text fields, checkboxes and what not, that the user has entered, before I send the request to the server to populate the second drop-down, I guess I have to send them back as well, so that the form does not get "reset". Am I right ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time you submit the form, the selected value from each select list gets send with the rest of the form information.

In that demo, I build the select list option tags in an option and then read all of them as one bean property but you could implement the same logic with JSTL and EL or (dare I say it...) scriptlets in your JSP.


[ May 18, 2005: Message edited by: Ben Souther ]
 
Sri Basavanahally
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

I am thinking of collecting the selected values(including text fields, checkboxes, radio buttons, etc) in a bean( I am using Struts) and placing the bean in request scope after the first form submit. I can use these values in the JSP to pre-popluate the form when it gets displayed the second time.

Is there an easier way ? :-) I will look into JSTL and EL.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic