PrimeFaces p:selectBooleanCheckbox
and p:selectBooleanButton
can be linked to a primitive data type boolean
variable in a controller.
I want to have a controller which can be linked to a reference data type Boolean
. It must be able to show the null
, true
and false
values.
The p:triStateCheckbox
can be linked to a String
variable, but not a Boolean
value.
Is there any JSF / PrimeFaces UI component which can represent reference data type Boolean
in a Java Controller?
Boolean
is now natively supported.
See https://github.com/primefaces/primefaces/issues/13244
You should write a converter for String
to Boolean
if you want to use the TriStateCheckbox
as explained in the documentation:
TriStateCheckbox passes values “0”, “1”, “2” by default for each state and this can be customized using a converter.
So, use something like:
public class TriStateBooleanConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null) {
return null;
}
switch (value) {
case "0": return null;
case "1": return Boolean.TRUE;
case "2": return Boolean.FALSE;
default: throw new ConverterException();
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Boolean bool = (Boolean) value;
if (bool == null) {
return "0";
}
return bool ? "1" : "2";
}
}
See also: