Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

h:selectOneMenu with boolean items doesn't work with null value

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!

like image 450
lukas Avatar asked Jul 25 '12 18:07

lukas


1 Answers

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
like image 52
BalusC Avatar answered Nov 09 '22 00:11

BalusC