webtier@glassfish.java.net

Converter for h:selectOneMenu

From: <webtier_at_javadesktop.org>
Date: Thu, 24 Dec 2009 03:01:01 PST

Hi,

I wrote a converter for my h:selectOneMenu

                                <h:selectOneMenu value="#{myBean.type}">
                                        <f:selectItems value="#{myBean.possibleTypes}" var="t" itemLabel="#{t.type}" itemValue="#{t.id}" />
                                        <f:converter converterId="MyConverter" />
                                </h:selectOneMenu>

possibleTypes is a List of object and the field "id" is of type Long.
If I submit my page I get the following validation error:
j_idt4:j_idt12: Validation Error: Value is not valid

IMO everthing is fine so I had a look at the source code of Mojarra 2.0.2. I found the following cod in java.faces.component.SelectUtils (Method private static Object doConversion(FacesContext ctx, ...)

...
            compareValue = itemValue;
            if (compareValue instanceof String
                 && !(value instanceof String)) {
                // type mismatch between the time and the value we're
                // comparing. Invoke the Converter.
                compareValue = converter.getAsObject(ctx,
                        component,
                        (String) compareValue);
            }
...

So the converter is only called if compareValue is a String. Unfortunately my compareValue is of type Long as mentioned above.

If I create a getter which retuns a String of my id e.g.

        public String getIdStr() {
                if(this.id != null) return this.id.toString();
                return null;
        }

and if I use this string-getter in my f:selectItems everything works fine

                                <h:selectOneMenu value="#{myBean.type}">
                                        <f:selectItems value="#{myBean.possibleTypes}" var="t" itemLabel="#{t.type}" itemValue="#{t.idStr}" />
                                        <f:converter converterId="MyConverter" />
                                </h:selectOneMenu>

I don't understand the String-type-check inside the method doConversion.
Do I have to use an additional Long-String converter?

Any ideas?

Alex
[Message sent by forum member 'alexander_bell' (Alexander.Bell_at_j4fry.org)]

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