Skip to main content

h:selectOneMenu with complex type for value

4 replies [Last post]
martyhall
Offline
Joined: 2009-08-19

I want to do this:

<h:selectOneMenu value="#{foo.color}">
<f:selectItems value="#{foo.colors}"/>
</h:selectOneMenu>

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@6c2a78' for 'null Converter'.

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

<h:selectOneMenu value="#{foo.count}">
<f:selectItems value="#{foo.counts}"/>
</h:selectOneMenu>

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

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Martin Beránek

I'am not sure, but I thing that you need converter for type Color. You
can register one globaly... (as in Jsf 1.x)

Martin

Dne 2.3.2010 03:25, webtier@javadesktop.org napsal(a):
> I want to do this:
>
> <h:selectOneMenu value="#{foo.color}">
> <f:selectItems value="#{foo.colors}"/>
> </h:selectOneMenu>
>
> 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@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:
>
> <h:selectOneMenu value="#{foo.count}">
> <f:selectItems value="#{foo.counts}"/>
> </h:selectOneMenu>
>
> 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@coreservlets.com)]
>
> http://forums.java.net/jive/thread.jspa?messageID=389538
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>

--
Martin Beránek
mailto:martin.beranek@i.cz
http://www.i.cz

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

Joel Weight

I find it very interesting that it worked with Integer, but not with Color.
Could that come down to auto boxing? I was seeing a similar problem to
yours using a selectManyListBox and had to write a custom converter. My
case was slightly different in that with a selectMany I was setting a
collection instead of a single value, so I attributed mine to type erasure
in generics. I was expecting a List but was getting a List.
If you want more info on my case you can read about it at
http://digitaljoel.wordpress.com/2010/01/11/jsf-2-custom-converter/
Fortunately,
converters (just like everything else) are even easier in JSF 2.

In any case I'd love to hear from the experts.

Joel

2010/3/1 Martin Beránek

> I'am not sure, but I thing that you need converter for type Color. You can
> register one globaly... (as in Jsf 1.x)
>
> Martin
>
> Dne 2.3.2010 03:25, webtier@javadesktop.org napsal(a):
>
> I want to do this:
>>
>> <h:selectOneMenu value="#{foo.color}">
>> <f:selectItems value="#{foo.colors}"/>
>> </h:selectOneMenu>
>>
>> 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@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:
>>
>> <h:selectOneMenu value="#{foo.count}">
>> <f:selectItems value="#{foo.counts}"/>
>> </h:selectOneMenu>
>>
>> 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@coreservlets.com)]
>>
>> http://forums.java.net/jive/thread.jspa?messageID=389538
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
>> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>>
>>
> --
> Martin Beránek
> mailto:martin.beranek@i.cz
> http://www.i.cz
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
>
[att1.html]

Ryan Lubke

Martin is correct. You'll need a converter to handle this case.
However, you don't *have* to explicitly assign a converter to the select
one menu (and incidentally, this is why it works for Integer), you can
instead register the converter by type:

@FacesConverter(forClass=Color.class)

When the Renderer attempts to deal with the SelectItems, it will first
look for an explicit converter and if not found, then it
will attempt to look up a Converter registered for the model type of the
value.

On 3/2/10 8:31 AM, Joel Weight wrote:
> I find it very interesting that it worked with Integer, but not with
> Color. Could that come down to auto boxing? I was seeing a similar
> problem to yours using a selectManyListBox and had to write a custom
> converter. My case was slightly different in that with a selectMany I
> was setting a collection instead of a single value, so I attributed
> mine to type erasure in generics. I was expecting a List but was
> getting a List. If you want more info on my case you can read
> about it at
> http://digitaljoel.wordpress.com/2010/01/11/jsf-2-custom-converter/ Fortunately,
> converters (just like everything else) are even easier in JSF 2.
>
> In any case I'd love to hear from the experts.
>
> Joel
>
> 2010/3/1 Martin Beránek >
>
> I'am not sure, but I thing that you need converter for type Color.
> You can register one globaly... (as in Jsf 1.x)
>
> Martin
>
> Dne 2.3.2010 03:25, webtier@javadesktop.org
> napsal(a):
>
> I want to do this:
>
> <h:selectOneMenu value="#{foo.color}">
> <f:selectItems value="#{foo.colors}"/>
> </h:selectOneMenu>
>
> 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@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:
>
> <h:selectOneMenu value="#{foo.count}">
> <f:selectItems value="#{foo.counts}"/>
> </h:selectOneMenu>
>
> 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@coreservlets.com )]
>
> http://forums.java.net/jive/thread.jspa?messageID=389538
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> webtier-unsubscribe@glassfish.dev.java.net
>
> For additional commands, e-mail:
> webtier-help@glassfish.dev.java.net
>
>
>
> --
> Martin Beránek
> mailto:martin.beranek@i.cz
> http://www.i.cz
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
>
> For additional commands, e-mail:
> webtier-help@glassfish.dev.java.net
>
>
>

[att1.html]

martyhall
Offline
Joined: 2009-08-19

Thanks, Ryan (and Martin); helpful as always. I think I was thrown off by the language in the SelectItem JavaDocs that describes the Object as "the value to be delivered to the model if this item is selected by the user" and a JSF 1.x book I read ages ago that suggested that all you need to do is have <h:selectOneMenu value="#{foo.color}"> and then have getColor and setColor return/take the object type. Clearly, I misunderstood all along. I'll use the converter.

Cheers-

- Marty
------------
JSF 2.0 Training Course: http://courses.coreservlets.com/public-courses/jsf2/