SelectOneMenu and InputText
Hi,
I want to do the following using JSF:
There should be a SelectOneMenu from which a user can select an entry. Next to the SelectOneMenu, I want to have two buttons: one to replace the text in a InputText field with the selected item and one to append the selected item to the current text in the InputText field.
I'm not quite sure what the right way is to do that. Should I use bindings on both components (SelectOneMenu and InputText) and then manipulate the values of the InputText-binding component in the action methods of the buttons? Or is this a bad idea (and, what are the alternatives)?
How can I find out which item was selected in the SelectOneMenu using the binding object (which is of type HtmlSelectOneMenu)? I tried to use getValue(), but this returned null... I would have expected something like getSelectedIndex() or getSelectedItem(), but did not find anything like that. Of course I could track all the updates and thereby always save the selected item in some bean member, but this looks like a hack to me...
Can somebody point me to the right methods?
Thanks in advance,
Michael
[1154 byte] By [
kuhnmia] at [2007-11-27 9:37:53]

# 1
> How can I find out which item was selected in the
> SelectOneMenu using the binding object (which is of
> type HtmlSelectOneMenu)? I tried to use getValue(),
> but this returned null...
well, your approch is correct, you just need the getValue method of the HtmlSelectOneMenu object in your backing bean. The value attribute of the
selectOneMenu will be filled with the selected value of the itemValue attribute.
ferudun
# 2
Gently use valuebindings instead of componentbindings. It's pretty straightforward.
Here is an untested example:<h:selectOneMenu value="#{myBean.selectedItem}">
<f:selectItems value="#{myBean.selectItems}" />
</h:selectOneMenu>
<h:inputText value="#{myBean.inputValue}" />
<h:commandButton value="replace" action="#{myBean.replace}" />
<h:commandButton value="append" action="#{myBean.append}" />
MyBeanprivate String selectedItem; // + getter + setter
private List<SelectItem> selectItems; // + getter
private String inputValue; // + getter + setter
public MyBean() {
fillSelectItems();
}
private void fillSelectItems() {
selectItems = new ArrayList<SelectItem>();
selectItems.add(new SelectItem("value1", "label1"));
selectItems.add(new SelectItem("value2", "label2"));
selectItems.add(new SelectItem("value3", "label3"));
}
public void replace() {
inputValue = selectedItem;
}
public void append() {
inputValue += selectedItem;
}