• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

How to set default selection for selectOneMenu?  RSS feed

 
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>
 
Bartender
Posts: 19814
93
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
Bartender
Posts: 19814
93
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
 
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()
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!