tags:

views:

1028

answers:

2

Hi,

I would like to bind a backing bean's field to the selected value of a selectOneListbox. This value could be null, so i want to convert this to 0. This will set the selected value to the "default" selectItem. I'm using JSF2

I'm planning to do this with the http://java.sun.com/jstl/core taglib (using <c:if test="#{empty...}>)

My question is: is there a "cleaner" way to do this. Maybe JSF(2) related taglib?

Thankyou!

+3  A: 

The "JSFish" way to do this would be to create a converter:

public Object getAsObject(FacesContext context, UIComponent comp, String param) {
    return (param.equals("0")) ? null : param;
}

public String getAsString(FacesContext context, UIComponent comp, Object obj) {
    return (obj == null) ? "0" : obj.toString();
}
Zack
Thank you for your suggestion. But my backing bean is also my Entity,Model Bean. I don't really want to add a transient field:)
Michael Bavin
In that case, my second suggestion would probably be best.
Zack
+3  A: 

Just use Long or Integer instead of String as item value. EL will automatically coerce number (and boolean) values from/to string.

BalusC
Yep, that'll work if he sticks to the wrapper types (`Integer` instead of `int`) - EL won't coerce `null` to a primitive.
McDowell