webtier@glassfish.java.net

h:selectOneMenu with complex type for value

From: <webtier_at_javadesktop.org>
Date: Mon, 01 Mar 2010 18:25:11 PST

I want to do this:

   &lt;h:selectOneMenu value="#{foo.color}"&gt;
    &lt;f:selectItems value="#{foo.colors}"/&gt;
  &lt;/h:selectOneMenu&gt;

The getColor and setColor methods return/take a Color object, where Color is a simple class I made up to test this. The getColors method returns a SelectItem[]. I tried making the SelectItem[] two different ways:

1) SelectItem[] colors = { new SelectItem(new Color(...)), ... };
In this case, I gave Color a toString method that spits out a color name like "red".

2) SelectItem[] colors = { new SelectItem(new Color(...), "red"), ... };

In both cases, it properly displays the color names ("red", "green", etc.) in the menu. But, in both cases, when I try to submit the form, I get

Conversion Error setting value 'myPackage.Color_at_6c2a78' for 'null Converter'.

I [u]can[/u] get this general process to work if I use Integer as the type. Ie, something like this works fine:

   &lt;h:selectOneMenu value="#{foo.count}"&gt;
    &lt;f:selectItems value="#{foo.counts}"/&gt;
  &lt;/h:selectOneMenu&gt;

Here, getCount and setCount get/take an Integer, and getCounts returns a SelectItem[] containing entries like

new SelectItem(1, "One")

I don't think the behavior has changed since JSF 1.x, so I think the problem is that I never understood how to do this in the first place. I was under the impression that this process should be straightforward without defining any converters; I thought that all I had to do was to make the type of the value of h:selectOneMenu match the Object type (first constructor argument) of each SelectItem. What am I doing wrong?

Cheers-

- Marty
[Message sent by forum member 'martyhall' (hall_at_coreservlets.com)]

http://forums.java.net/jive/thread.jspa?messageID=389538