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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.