Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

How to set default selection for selectOneMenu?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Saloon Keeper
Posts: 24193
166
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 24193
166
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim, it works now:)
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I solved this by using on change=submit()
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic