aspose file tools*
The moose likes JSP and the fly likes populate combox with list with default selected value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "populate combox with list with default selected value" Watch "populate combox with list with default selected value" New topic
Author

populate combox with list with default selected value

Daniel Conner
Ranch Hand

Joined: Jan 25, 2007
Posts: 47
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

Joined: Jan 10, 2002
Posts: 61437
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Daniel Conner
Ranch Hand

Joined: Jan 25, 2007
Posts: 47
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

Joined: Jan 10, 2002
Posts: 61437
    
  67

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

Joined: Jan 25, 2007
Posts: 47
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: populate combox with list with default selected value