File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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: 16305
    
  21

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: 16305
    
  21

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()
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set default selection for selectOneMenu?