H: selectOneMenu with boolean elements does not work with a null value

I am working with JSF 2.0, JBoss 7.1.1 Final, and I have the following problem with selectOneMenu. I want to be able to set the field in the managed bean to true / false / null. So I created the following selectOneMenu:

<h:selectOneMenu value="#{personList.criteria.registrationComplete}" > <f:selectItem itemValue="#{null}" itemLabel="Any.." /> <f:selectItem itemValue="true" itemLabel="Yes"/> <f:selectItem itemValue="false" itemLabel="No"/> </h:selectOneMenu> 

Now, if I select β€œAny ..”, it will assign β€œfalse” to the registrationComplete field (which is logical). Therefore, null is interpreted as false . I also tried using booleans in selectItem (s), namely:

  <h:selectOneMenu value="#{personList.criteria.registrationComplete}" > <f:selectItem itemValue="#{null}" itemLabel="Any.." /> <f:selectItem itemValue="#{true}" itemLabel="Yes"/> <f:selectItem itemValue="#{false}" itemLabel="No"/> </h:selectOneMenu> 

And I also registered the converter in faces-config as follows:

 <converter> <converter-id>booleanConverter</converter-id> <converter-class>javax.faces.convert.BooleanConverter</converter-class> </converter> 

and tried to use it:

 <h:selectOneMenu value="#{personList.criteria.registrationComplete}" > <f:selectItem itemValue="#{null}" itemLabel="Any.." /> <f:selectItem itemValue="true" itemLabel="Yes"/> <f:selectItem itemValue="false" itemLabel="No"/> <f:converter converterId="booleanConverter"/> </h:selectOneMenu> 

But all these attempts led to the same behavior - when a zero value was chosen, it was interpreted as false.

I debugged it and in the stack trace I found a place where this happens. In AstValue.setValue(EvaluationContext, Object) line: 204

he calls

 ELSupport.coerceToType(value, targetClass) 
Parameter

value is null and targetClass is boolean. This coerceToType method returns false.

Any ideas how to solve these problems? Thanks!

+8
boolean el jsf-2 selectonemenu
source share
1 answer

This is typical of the Apache EL parser, which is used by Tomcat and JBoss. It is known that primitives and their representations of shell objects should not be distinguished when forcing null values ​​in EL. Wrapper types are always considered primitives. For example, it works great in Glassfish.

You can disable the behavior of this Apache EL parser by adding the following VM argument to the launch of your server script:

 -Dorg.apache.el.parser.COERCE_TO_ZERO=false 
+8
source share

All Articles