JavaServer Faces - How to get the selected value in SelectOneMenu in backing bean
Hello all,
I need your help. I want to have 2 select menus with the second menu's items list are populated based on the selection in the first menu. I don't know how to get the selected value in the backing bean so that I can based on that select menu to populate the second menu's item list. Basically I need to access to the UI Component of the first select Menu and retrieve its selected value.
Could you help me out?
Thank you very much in advance,
Lngo
[491 byte] By [
lngo77a] at [2007-11-26 23:19:00]

# 1
You can just retrieve it by the valuebinding of the component. You don't need componentbinding.
<h:selectOneMenu value="#{myBean.selectedItem}"><f:selectItems value="#{myBean.selectItems}" />
</h:selectOneMenu>
...
<h:commandButton value="submit" action="#{myBean.submit}" />
private T selectedItem; // + getter + setterpublic void submit() {
System.out.println(selectedItem); // the selectedItem is just available in here.
}
# 2
Hi BalusC,
Thanks for a very quick response. Probably my post subject is misleading. Actually what I want is to use the selection on the fist select menu to fill in the menu items of the second select menu. So what I want is like the second select menu is dependent of the first menu selected item. So I need to get the value selected right after it's being selected and use it to dynamically populate the second menu item lists.
In addition, I would also to ask for help about how to do that if I have the select menu inside of a dataTable. In that case, how do I have the select menu binding to a property in the backing bean? Would I need to use an array of HTMLSelectMenu in the backing bean?
Thanks a lot,
Lngo
# 3
Hi Lingo,
There r two ways of getting the values into the list. First one is hardcoding the values and the second one is use the list and get the values into the list by firing a query in the database.
Inorder to display the values in the second menu based on the first onces selection we need to add an attribute to the first selectonemenu known as valueChangeListener and we need to sumit the page.
Here is the sample code
<h:selectOneMenu id="catalogue"
binding="#{urbean.catalogue}" onchange="submit()"
valueChangeListener="#{urbean.categoryValueChange}"
<f:selectItem itemLabel="Select Catalogue" itemValue="" />
<f:selectItems value="#{urbean.catalogueList}" />
</h:selectOneMenu>
<h:selectOneMenu id="category"
binding="#{urbean.category}"><f:selectItem itemLabel="Select Category" itemValue="" />
<f:selectItems value="#{urbean.categoryList}" />
<</h:selectOneMenu>
****************************************************************************
now in method called by valuechangelistener we need to write the similar code
public void categoryValueChange(ValueChangeEvent event) {
String rfnum = (String) event.getNewValue();
List categoryList = new ArrayList();
List tempList = new TablenameDAO().getActiveCatByCatalogueID(rfnum);
for (int i = 0; i < tempList.size(); i++) {
Tablename tablename = (Tablename ) tempList.get(i);
String value = "" + tablename .getrfnum();
String label = tablename .getname();
if (label == null) {
label = "";
}
SelectItem item = new SelectItem(value, label);
categoryList.add(item);
}
bean.setCategoryList(categoryList);
}
///getActiveCatByCatalogueID (rfnum) should bring the records which r based on the rfnum
**************************************************************************
if u follow this process i am damsure that u will get the values in to the secondlist based upon the first list
Thanks & Regards
Manidhar
# 4
Thanks for a very quick response. Probably my post
subject is misleading. Actually what I want is to use
the selection on the fist select menu to fill in the
menu items of the second select menu. So what I want
is like the second select menu is dependent of the
first menu selected item. So I need to get the value
selected right after it's being selected and use it
to dynamically populate the second menu item lists.
I have shown how you can get the selected value of a selectOneMenu. Just use this value to populate the selectItems of the second selectOneMenu. It's pretty straightforward:
public void getSelectItemsForSecondMenu() {if (selectedItemFromFirstMenu != null) {
loadSelectItemsForSecondMenu(selectedItemFromFirstMenu); // Populate here
}
return selectItemsForSecondMenu;
}
In addition, I would also to ask for help about how
to do that if I have the select menu inside of a
dataTable. In that case, how do I have the select
menu binding to a property in the backing bean? Would
I need to use an array of HTMLSelectMenu in the
backing bean?
Bind the selectedItem to the row object and bind selectItems to the backing bean.
# 7
Hi BalusC,
Could you elaborate more about the row object? I think that might be what I need. I need the second menu items be populated based on the selected value of the first menu on THE SAME ROW. But I have now is the second menu's select items binding have the scope of whole dataTable.
Please show me more specific how to put the select items in a row scope.
This is what I have
<h:dataTable id="tbl" value="#{advisingForm.formData.courses}" var="cc" ><h:column>
<f:facet name="header">
<h:outputText value="Course" style="text-decoration: underline; font-weight: bold"/>
</f:facet>
<h:selectOneMenu id="dept" value="#{cc.dept}" valueChangeListener="#{advisingForm.courseChangeListener}">
<f:selectItems value="#{advisingForm.subjectList}"/>
<a4j:support event="onchange" action="#{advisingForm.dummy}" reRender="tbl"/>
</h:selectOneMenu>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Number" style="text-decoration: underline; font-weight: bold"/>
</f:facet>
<h:selectOneMenu id="number" value="#{cc.number}" valueChangeListener="#{advisingForm.numberChangeListener}">
<f:selectItems value="#{advisingForm.numberList}"/>
</h:selectOneMenu>
</h:column>
</h:dataTable>
The problem is the advisingForm.numberList is shared for all rows, when it's changed on 1 row, all other row see the change even those row's first select menu is not selected yet.
Tons of thanks,
LNgo