permaculture playing cards*
The moose likes JSF and the fly likes How to set default selection for selectOneMenu? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to set default selection for selectOneMenu?" Watch "How to set default selection for selectOneMenu?" New topic
Author

How to set default selection for selectOneMenu?

ya ji
Ranch Hand

Joined: Jul 14, 2008
Posts: 40
Code:
<h:selectOneMenu id="selOrg" value="#{MyBean.orgId}">
<f:selectItem itemLabel="Select..." itemValue=""/>
<f:selectItems value="#{MyBean.allOrgSelectItems}" />
</h:selectOneMenu>
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952
    
  19

The "default" selection will be whatever the current value of orgId in MyBean is.

I don't recommend starting bean names with upper case ("MyBean"). It's legal, but in Java, you normally capitalize the first character of a class, not an instance, so it's potentially confusing to others.


Customer surveys are for companies who didn't pay proper attention to begin with.
ya ji
Ranch Hand

Joined: Jul 14, 2008
Posts: 40
Thanks for your suggestion, It has a proper name, I changed it to MyBean when pasting to here
ya ji
Ranch Hand

Joined: Jul 14, 2008
Posts: 40
Hi Tim,

the orgId has value, but the default selection is still the first item.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952
    
  19

Check your selections list. The SelectItem has 2 significant parts, which are the text displayed and the value returned for the text. I believe there's a single-element constructor which sets both items to the same value, as well as a double-element constructor so you can pair things like "FL", Florida.

One problem I always have using the 2-element constructor is remembering which element is the display value and which is the data value.

Another thing that can be a problem is having the right get/set methods. If the method signatures are incorrect, you won't get what you want.

The value of a selection can have just about any simple data type. Most commonly, it's a String, but I also use integer values. Because of the possibility for a null value, I use a wrapper for that, like so:

ya ji
Ranch Hand

Joined: Jul 14, 2008
Posts: 40
Thanks Tim, it works now
madupathi arun
Greenhorn

Joined: Feb 25, 2008
Posts: 23
hi yaji
I am unable to set the default value for h:selectOneMenu

my jsf is

<h:selectOneMenu id="requestStatus12" value="#{currentLanguageResolver.requestState}" onchange="submit()" valueChangeListener="#{currentLanguageResolver.valueChangeSelectOneMenu}">

<f:selectItems value="#{currentLanguageResolver.possibleStates}"/>
</h:selectOneMenu>

my currentLanguageResolver is
==========================

public List<SelectItem> getPossibleStates() {
RequestLanguageState[] states = RequestLanguageState.values();
List<SelectItem> result = new ArrayList<SelectItem>(states.length);
result.add(new SelectItem("", ""));
myMap.clear();
for (RequestLanguageState state : states) {
myMap.put(state.toString(), state);
// result.add(new SelectItem(state, new I18n(state.toString()).getDefault()));
result.add(new SelectItem(state.toString(), new I18n(state.toString()).getDefault()));
}
return result;
}
madupathi arun
Greenhorn

Joined: Feb 25, 2008
Posts: 23
Hi I solved this by using on change=submit()
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to set default selection for selectOneMenu?
 
Similar Threads
Getting this error when binded with a list for selectItems in jsf1.2_10
Issue with my webApp
Conversion Error setting value ''{0}'' for ''{1}''
selectOneMenu ValueChangeListener not Firing
JSF Dropdown list