43

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
dws
  • 583
  • 1
  • 5
  • 10

3 Answers3

104

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • http://docs.oracle.com/cd/E17802_01/j2ee/j2ee/javaserverfaces/1.1_01/docs/api/index.html Says da a NullPointerException should be thrown when the value is null. – bruno Jul 30 '14 at 17:39
  • 5
    @bruno, that documentation is for JSF 1.1, released +10 years ago. Please update your bookmarks. – DavidS Apr 09 '15 at 16:45
  • @BalusC, it doesn't work with converter - It throws NullPointerException (JSF 2.2). – guest Jun 25 '15 at 14:22
  • @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. – BalusC Jun 25 '15 at 14:23
  • @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. – Parkash Kumar Aug 05 '15 at 07:02
  • Does hideNoSelectionOption work for primefaces ? – user2918640 Feb 29 '16 at 07:12
10

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
rad
  • 1,106
  • 7
  • 15
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"
A. Qaoud
  • 33
  • 2
  • You can use this solution @user2918640 – A. Qaoud Jul 19 '17 at 11:58
  • @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... – A. Qaoud Jul 19 '17 at 12:33
  • 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. – BalusC Jul 19 '17 at 16:30
  • @BalusC : hideNoSelectionOption="true" and noSelectionOption="true" attributes don't have an effect in case you used – A. Qaoud Jul 20 '17 at 09:12