Skip to main content

Permit empty/no/null selection in h:selectOneMenu

2 replies [Last post]
ringerc
Offline
Joined: 2008-06-08
Points: 0

This may be so obvious I'm just looking right past it, but, in JSF2 (Mojarra, Glassfish 3.0.1):

If I have a h:selectOneMenu control in a JSF2 page with a value bound to a List, is there any way to permit an empty/null selection? The menu defaults to having the first item selected when it's rendered, and doesn't have any blank or placeholder first row, even if required=false .

I can insert a dummy value in the list and alter my FacesConverter to handle it, but that seems awfully ugly for something that's a pretty basic piece of functionality. Is there some simple way I'm missing?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
frank_langelage
Offline
Joined: 2006-07-12
Points: 0

The List<...> is probably used for .
How about this?




ringerc
Offline
Joined: 2008-06-08
Points: 0

Thanks for your reply and for taking the time to read my frustrated whining. You've helped me catch a mistake I'd made that was going to cost me a great deal of pointless stuffing about.

Your suggestion was my first thought, too. When I tried it, Mojarra didn't seem to like it, throwing an exception (which I unfortunately failed to retain record of) about being unable to cast String to SelectItem. I concluded that it didn't support using f:selectItem and f:selectItems together in the same body.

Well, that was stupid.

My only defense is that I was tired and somewhat frustrated by other things. I'd made an accidental edit that caused the "value" property to return a String, with naturally wasn't going to work very well. I then got off on a tangent looking at the noSelectionValue attribute of f:selectItems, which I thought might hold the solution, but is missing in in Mojarra's taglib and the public javadoc due to a copy'n'paste error (https://javaserverfaces.dev.java.net/issues/show_bug.cgi?id=1793). As the last few issues I've hit have turned out to be Glassfish or Mojarra bugs I was too quick to assume that this, too, was a bug/limitation in the tools.

For anyone else finding this question: if you want to permit selection of "nothing" or "empty" in a f:selectOneMenu that's backed by a f:selectItems of a domain collection, here's how:

[code]




[/code]

Anyway, now I find myself wondering why the NetBeans code generator is doing it in such a convoluted way. I don't use the generated code, but it can be useful for producing a skeleton of parts of the app as placeholders, and it was the way the generated code does it that reinforced my suspicion that maybe JSF2 didn't have an obvious and simple way of doing this.

Thanks for making me re-examine that assumption before I wasted a huge amount of time on it.