문제

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!

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top