I'm working with JSF 2.0, JBoss 7.1.1 Final and I have following problem with selectOneMenu. I want to be able to set a field in a managed bean to true/false/null. Thus, I have created 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 choose to 'Any..', it will assign "false" to the registrationComplete field (which is Boolean). So null is interpreted as false. I also tried to use boolean values in the selectItem(s), that is:
<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 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 resulted in the same behavior -- when the null value was selected, it was interpreted as false.
I debugged it and in the stack trace I found the location where it happens. In AstValue.setValue(EvaluationContext, Object) line: 204
it calls
ELSupport.coerceToType(value, targetClass)
value parameter is null and targetClass is Boolean. This coerceToType method then returns false.
Any ideas how to solve this issues? Thanks!
This is typical to the Apache EL parser which is used by Tomcat and JBoss. It's known to not distinguish between primitives and its wrapper object representations when coercing null
values in EL. The wrapper types are always treated as primitives. It works fine in Glassfish, for example.
You can turn off this Apache EL parser behaviour by adding the following VM argument to your server startup script:
-Dorg.apache.el.parser.COERCE_TO_ZERO=false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With