Best way to add a “nothing selected” option to a selectOneMenu in JSF

Just explicitly set the select item value to null.

<h:selectOneMenu value="#{bean.selectedItem}">
    <f:selectItem itemValue="#{null}" itemLabel="--select--" />
    <f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>

No, an empty string like itemValue="" is not sufficient. It really has to be null. Otherwise you run into trouble as described in this Q&A: Using a “Please select” f:selectItem with null/empty value inside a p:selectOneMenu.

If the item happen to be required="true" and you’re using JSF 2.x, then you could add noSelectionOption="true" to the select item. This is only useful if you also set hideNoSelectionOption="true" on the selection component. It will then hide the empty option in the list once the enduser selects a different item, hereby making it impossible to re-select the empty option.

<h:selectOneMenu value="#{bean.selectedItem}" hideNoSelectionOption="true">
    <f:selectItem itemValue="#{null}" itemLabel="--select--" noSelectionOption="true" />
    <f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>

See also page 114 of The Definitive Guide to JSF under section “SelectItem tags”:

Note that a select item with value of #{null} can be used to present the default selection in case the bean property associated with selection component’s value attribute is null. If you have consulted the tag documentation of <f:selectItem>, then you’ll perhaps have noticed the noSelectionOption attribute and have thought that it was intended to represent a “no selection option”. Actually, this isn’t true. Many starters indeed think so, as you can see in many forums, Q&A sites, and poor-quality tutorials on the Internet. In spite of the misleading attribute name, it does not represent a “no selection option”.

A better attribute name would have been hideWhenOtherOptionIsSelected, and even then it works only when the parent selection component has explicitly a hideNoSelectionOption="true" attribute set. So, hideWhenOtherOptionIsSelectedAndHideNoSelectionOptionIsTrue would ultimately have been the most self-explanatory attribute name. Unfortunately, this wasn’t very well thought out when the noSelectionOption was implemented in JSF 1.2. Requiring two attributes for this attribute to function shouldn’t have been necessary. The primary purpose of this attribute pair is to prevent the web site user from being able to re-select the “no selection option” when the component has already a non-null value selected. For example, by having it prepared in a @PostConstruct method, or by re-rendering the component after a form submit with a non-null value.

Copyright disclaimer: book is written by me.

Leave a Comment