set a selected value in a SelectOneMenu

has anyone been successful in setting a value in a selectonemenu. I have a record that when it is populated on the page one of the values originally came from a dropdownmenu. I would like to use the same menu when they edit the record and have the value from record set as the current item in the list.

<h:selectOneMenu styleClass="selectOneMenu" id="menu1">

<f:selectItems value="#{selectitems.qualDrop.rows.QUALIFIER_QUAL_TYPE.QUALIFIER_QUAL_TYPE.toArray}" />

Please point me in the right direction.

Thanks!

[628 byte] By [DeAltona] at [2007-11-27 6:28:17]
# 1
Set it in the valuebinding of the h:selectOneMenu component.<h:selectOneMenu value="#{myBean.selectedItem}">
BalusCa at 2007-7-12 17:51:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I tried it with no success

<h:selectOneMenu styleClass="selectOneMenu" id="menu2" value="#{pc_RjTestPage.myItem}">

<f:selectItems value="#{selectitems.qualDrop.rows.QUALIFIER_QUAL_TYPE.QUALIFIER_QUAL_TYPE.toArray}

I set the value to what should be the 4th item in the list and with MyItem gets

changed to the first item in the list...">

DeAltona at 2007-7-12 17:51:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Any help would be great!!!
DeAltona at 2007-7-12 17:51:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I am currently prototyping an application which uses a selectonemenu tag. One thing I noticed is that the selection only seemed to work if the value you were selecting was part of the list of options. My code follows:

<h:selectOneMenu id="loc" value="#{link.linkLocation}" title="select any one in this menu">

<f:selectItem id="si1" itemLabel="Sales Ideas" itemValue="Sales Ideas" />

<f:selectItem id="si2" itemLabel="My Area" itemValue="My Area" />

<f:selectItem id="si3" itemLabel="Another Area"

itemValue="Another Area" />

<f:selectItem id="si4" itemLabel="Your Desk" itemValue="Your Desk" />

<f:selectItem id="si5" itemLabel="Big Boss" itemValue="Big Boss" />

<f:selectItem id="si6" itemLabel="Downtown" itemValue="Downtown" />

</h:selectOneMenu>

In this example #{link.linkLocation} returns a String containing one of the values listed in the f:selectItem list. Then name is not as important as the value. My selections would not work until my returned String matched one of the itemValue entries. Now it works like a champ, although I still need to modify this code to remove the hard coded values. However, this is an initial proof-of-concept prototype so there's nothing fancy about it.

Michael_Johnson_COa at 2007-7-12 17:51:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
So this example is for returning a string once a selection has been made, correct?
DeAltona at 2007-7-12 17:51:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

This example sets a default selection in a SelectOneMenu component. So if #{link.linkLocation} returns the String "My Area" then when the page is rendered, the selection with an itemValue of "My Area" will show as selected.

I'm not sure what you mean by returning a String. The only thing in this example that returns a String is the EL expression #{link.linkLocation}. This is only useful to set a default selected value. To return a value from the component, you must make a selection and then query the component for its selected value. In this case, the selected value will be a String.

I'm not trying to confuse the issue, but I want to be clear about what's happening and not just give a default "yes" answer.

Michael_Johnson_COa at 2007-7-12 17:51:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Thanks for the help you clued me in the right direction. When I pulled the information from the db2 table Java wrapped it in an object array. Now it works great.

Thanks to all for your help!!!!!!

<h:selectOneMenu styleClass="selectOneMenu" id="newType"

value="#{pc_DailyReg.selectedQual}">

<f:selectItemsvalue="#{selectitems.pc_DailyReg.drop.drop.toArray}" />

</h:selectOneMenu>

public String[] getDrop() throws SQLException {

QualDrop q=new QualDrop();

q.execute();

QualDropRow[] qr= q.getRows();

drop = new String[qr.length];

for (int i = 0; i < qr.length; i++) {

drop[i] = qr[i].getQUALIFIER_QUAL_TYPE().toString().trim();

}

return drop;

}

DeAltona at 2007-7-12 17:51:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...