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 + setter

public void submit() {

System.out.println(selectedItem); // the selectedItem is just available in here.

}

BalusCa at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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

lngo77a at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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

mandihara at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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.

BalusCa at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Thank you both for the help. I am able to populate the second select menu items dynamically based on the first menu selected value and rerender it with ajax4jsf help. Now I have the problem with the select menu is being inside a dataTable. So if a user select 1 value in the first menu on the 1st row, the second menu would be populated with list of choices, and this new list of choices would also shown on the 2nd row when the whole table is rerendered by ajax4jsf, while the user has not make any selection on the second row's first menu. How could I seperate the 2 rows? I am currenyly binding the second selectItems to the same property in backing bean and that's why once its updated on first row, the second row will have the same value. How can I have 2 seperate selectItems binding to seperate rows in dataTable? I find this hard because row in dataTable is generated not controlled by my code.

Thanks a lot,

Lngo

lngo77a at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Then bind the selectitems to the row object also. It all depends on the scope where the items need to be shown.

Try to view it from an object oriented perspective. Does the selectitems depend on the row? Put it in the row object. Does the selectitems depend on request? Put it in a request scoped managed bean. Does the selectitems depend on session? Put it in a session scoped managed bean. Does the selectitems never change? Put it in a static var or enum or application scoped bean.

BalusCa at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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

lngo77a at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
The row object is the "cc" as declared in your code.
BalusCa at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
How could I retriecve the current row object in the backing bean? I need to be able to get the correct object in the list that corresponds to the current row to fill out the select items. Thanks for your always quick response.-Lngo
lngo77a at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

HtmlDataTable#getRowData().

public void getDataTableSelectItems() {

Object row = htmlDataTable.getRowData();

...

}

Also see http://balusc.xs4all.nl/srv/dev-jep-dat.html

BalusCa at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
Thanks BalusC for the great information on the site. I found the site from another post and was reading it.Thanks again.Lngo
lngo77a at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
Thank you so much. It works like a charm.-Lngo
lngo77a at 2007-7-10 14:21:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...