Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

populate combox with list with default selected value

 
Daniel Conner
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an editable table. The table is populated with values when the page opens. I have a couple comboboxes that I populate. My problem isn't with populating the combobox. My problem is the value that Im getting from the database(I am positive I am getting the right value...) is not selected value when the page opens. Below is the code I am using to populate the combo box... How do I populate it and use the value I am getting from my bean to be the selected value when I open the page? Thanks.



The value I want to use is located at: ${lmgmt.uoi}

Thanks again!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First a terminology correction: it''s not a combobox (or even a combox). A ComboBox is a desktop control not available in HTML. Call it a select or a dropdown.

So how would you code the <select> and its options to select on of the options if you were wirting it in straight HTML?
 
Daniel Conner
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be something like this:



I had tried doing something like that before, but with using expression language of course for the list and the variable. It printed the value from my bean twice. The first time it printed the value is the selected value and the second time it printed it from the list. It looked something like this:



Would it make sense to use <c:if to make sure that only the values from the list that are included in the list are the ones that aren't selected?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using JSP 2.0 you can use the ternary operator (?) of the EL to conditionally evaluate an expression. This makes it easy to conditionally include or exclude the "selected" attribute from the <option> based upon whether the values match or not.

You can also do it with <c:if> but it's much wordier.
 
Daniel Conner
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! Got it. Thanks Bear. You have been a lot of help... Not just on this topic, but also on other questions I have asked lately... Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic