• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set default selection for selectOneMenu?

 
ya ji
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code:
<h:selectOneMenu id="selOrg" value="#{MyBean.orgId}">
<f:selectItem itemLabel="Select..." itemValue=""/>
<f:selectItems value="#{MyBean.allOrgSelectItems}" />
</h:selectOneMenu>
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ya ji
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggestion, It has a proper name, I changed it to MyBean when pasting to here
 
ya ji
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

the orgId has value, but the default selection is still the first item.
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim, it works now
 
madupathi arun
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I solved this by using on change=submit()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic