webtier@glassfish.java.net

Re: [webtier] h:selectOneMenu with complex type for value

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Tue, 02 Mar 2010 10:13:43 -0800

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<Key> but was
> getting a List<String>. 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 <martin.beranek_at_i.cz <mailto:martin.beranek_at_i.cz>>
>
> 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_at_javadesktop.org
> <mailto:webtier_at_javadesktop.org> napsal(a):
>
> 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 <mailto:hall_at_coreservlets.com>)]
>
> http://forums.java.net/jive/thread.jspa?messageID=389538
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> webtier-unsubscribe_at_glassfish.dev.java.net
> <mailto:webtier-unsubscribe_at_glassfish.dev.java.net>
> For additional commands, e-mail:
> webtier-help_at_glassfish.dev.java.net
> <mailto:webtier-help_at_glassfish.dev.java.net>
>
>
> --
> Martin Beránek
> mailto:martin.beranek_at_i.cz <mailto:martin.beranek_at_i.cz>
> http://www.i.cz
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe_at_glassfish.dev.java.net
> <mailto:webtier-unsubscribe_at_glassfish.dev.java.net>
> For additional commands, e-mail:
> webtier-help_at_glassfish.dev.java.net
> <mailto:webtier-help_at_glassfish.dev.java.net>
>
>