tags:

views:

16

answers:

1

I am working on a jsp page which has a combo box:

<h:selectOneMenu disabled="#{controller.disabled}" id="actualType" value="#{controller.active.actualType}">
        <f:selectItem itemLabel="  " itemValue=" " />
        <f:selectItems id="ACTUAL" value="#{controller.types}" />
</h:selectOneMenu>

I need to get both the value and label for the currently selected Item. I tried adding binding to the but that always stays and the null value. If i put binding on the or on the i dont see a way to actual get the label from the currently selected Item.
Im sure it is something simple I am missing, any help would be greatly apperciated.

-edit-
thinking about the issue, I could just iterate over my list of selectItems and find the one whose value match the current selected value, then return that items description. While that would work, I would think there would be a more elegant solution.

+1  A: 

That's how HTML works. The <select> element will only send the value attribute of the selected <option> element.

But that shouldn't be a big issue. You namely already know both the value and label in the server side, inside the #{controller.types}. All you need to do to get the associated label is to get it by the value as key. I suggest to make it a Map which in turn can also be used in f:selectItems.

Basic kickoff example:

public class Bean {
    private String selectedItem; // +getter +setter
    private Map<String, String> items; // +getter

    public Bean() {
        items = new LinkedHashMap<String, String>();
        items.put("value1", "label1");
        items.put("value2", "label2");
        items.put("value3", "label3");
    }

    public void submit() {
        String selectedLabel = items.get(selectedItem);
        // ...
    }
}

with

<h:selectOneMenu value="#{bean.selectedItem}">
    <f:selectItem itemLabel="" itemValue="" />
    <f:selectItems value="#{bean.items}" />
</h:selectOneMenu>
BalusC
wow, you answered with more or less the same idea I had about iterating over the list, as I posted that edit. Thanks, making it a map is actually a really good idea.
SomeFatMan
You're welcome.
BalusC
and this OP doesn't have the vote option.. :)
Bozho