users@javaserverfaces.java.net

selectItem and conversion question

From: Guy Coleman <guy.coleman_at_prolog.uk.com>
Date: Wed, 07 Feb 2007 10:10:16 +0000

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?

Thanks,
-Guy Coleman.