Best way to add a "nothing selected" option to a selectOneMenu in JSF

59,186

Solution 1

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.

Solution 2

Add a single selectItem with null value;

<h:selectOneMenu value="#{bean.question}" required="true" requiredMessage="Please select a question">
    <f:selectItem itemValue="#{null}" itemLabel="Select" />
    <f:selectItems value="#{bean.questions}" />
</h:selectOneMenu>

Solution 3

We can in primefaces (when we have to use <p:selectOneMenu... from some reason like using <p:ajax..) add the following empty item:

<f:selectItem itemValue="#{null}" itemLabel="--select--"  itemDisabled="#{Mybean.value ne null}" />

Note: In such case we don't need the following two tags:

hideNoSelectionOption="true"

and

noSelectionOption="true"
Share:
59,186

Related videos on Youtube

dws
Author by

dws

Updated on July 09, 2022

Comments

  • dws
    dws almost 2 years

    I was wondering what would be the best or easiest way to allow a user to select nothing in a selectOneMenu.

    My example: I have a list of registered users and the administrator should be able to filter the list of displayed users by some criterias. These criterias, like the usertype (employee, customer, ...) can be chosen by selectOneMenus, like this:

    <h:selectOneMenu value="#{myBean.selectedUsertype}" converter="#{usertypeConverter}">
    <f:selectItems value={myBean.usertypes}" />
    </h:selectOneMenu>
    

    When the corresponding selectOneMenu is being backed by a list of POJOs using a converter, how can I add an item to the list indicating that the user didn't choose any specific item? Currently I have a dummy usertype object displaying the label "---", but this is causing several problems in other areas of my application and I don't think that this is the best solution.

  • bruno
    bruno almost 10 years
    docs.oracle.com/cd/E17802_01/j2ee/j2ee/javaserverfaces/1.1_0‌​1/… Says da a NullPointerException should be thrown when the value is null.
  • DavidS
    DavidS about 9 years
    @bruno, that documentation is for JSF 1.1, released +10 years ago. Please update your bookmarks.
  • guest
    guest about 9 years
    @BalusC, it doesn't work with converter - It throws NullPointerException (JSF 2.2).
  • BalusC
    BalusC about 9 years
    @guest: That's just a bug in the converter in question. Press "Ask Question" button if you have no idea how to fix that or couldn't find questions which answers how to create a proper converter.
  • Parkash Kumar
    Parkash Kumar almost 9 years
    @BalusC, itemValue="#{null}" or itemValue="null" is working fine when the items list is of type object. What about if the items list value is of type int? It throws java.lang.IllegalArgumentException for selected values incase of validation.
  • user2918640
    user2918640 over 8 years
    Does hideNoSelectionOption work for primefaces <p:selectOneMenu>?
  • A. Qaoud
    A. Qaoud almost 7 years
    You can use this solution @user2918640
  • A. Qaoud
    A. Qaoud almost 7 years
    @Kukeltje : The "select" label will remain on the menu after choosing another value.But in my solution, the "select" label will be disabled after choosing any other option...
  • BalusC
    BalusC almost 7 years
    This post does not add anything new to the already given answers. If you agree an answer, just upvote it instead of repeating it. Stack Overflow isn't like an old fashioned discussion forum wherein everyone repeats each other into an undigestable mess.
  • A. Qaoud
    A. Qaoud almost 7 years
    @BalusC : hideNoSelectionOption="true" and noSelectionOption="true" attributes don't have an effect in case you used <p:selectOneMenu instead of <h:selectOneMenu which user2918640 asked about as a comment on your solution.I would have added just a comment after him but I don't have enough reputation to do so as I'm new to stackoverflow. So, I thought that it's a good idea to post it as solution instead.I wasn't trying to repeat you and cause an "undigestable mess" as you have said.Finally, thanks a lot for your posts as you have helped a lot in JSF...