Question

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!

Was it helpful?

Solution

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();
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top