users@javaserverfaces.java.net

Re: selectItem and conversion question

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Thu, 08 Feb 2007 07:36:56 -0800

Guy Coleman wrote:
> I have a question about <f:selectItem />s and conversion with JSF 1.1_01.
>
> Given the following code:
>
> public class TestBean {
> private int value;
>
> public int getValue() {
> return value;
> }
> public void setValue(int value) {
> this.value = value;
> }
> }
>
> and JSP:
>
> <h:form>
> <h:selectOneMenu value="#{TestBean.value}" required="true">
> <f:selectItem itemValue="1" itemLabel="1" />
> <f:selectItem itemValue="2" itemLabel="2" />
> <f:selectItem itemValue="3" itemLabel="3" />
> </h:selectOneMenu>
> ...
> </h:form>
>
> This will fail validation when submitting the form.
>
> I'm guessing that this is because the selectOneMenu's value is being
> converted to an Integer, then validated by comparing against the
> values of the select items. The f:selectItem's values are Strings not
> Integers, so the validation fails.
>
> The behaviour is the same with MyFaces 1.1.5 (though not with earlier
> MyFaces versions - maybe they fixed something?).
>
> I was just going to work around this by generating SelectItems in code
> with Integer values, but my question is: is this expected behaviour?
> Are there any better ways to work around it?
Yes, in 1.1, this is expected. The model type is String since you
defined the select items as model type String.

In 1.2, coercion rules were added to make this case simpler for the
developer.
>
> Thanks,
> -Guy Coleman.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_javaserverfaces.dev.java.net
> For additional commands, e-mail: users-help_at_javaserverfaces.dev.java.net
>