h:带有布尔项的selectOneMenu不能使用空值

luk*_*kas 8 boolean el selectonemenu jsf-2

我正在使用JSF 2.0,JBoss 7.1.1 Final,我遇到了selectOneMenu的问题.我希望能够将托管bean中的字段设置为true/false/null.因此,我创建了以下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>
Run Code Online (Sandbox Code Playgroud)

现在,如果我选择'Any ..',它将为registrationComplete字段(布尔值)分配"false".所以null被解释为false.我还尝试在selectItem中使用布尔值,即:

 <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>
Run Code Online (Sandbox Code Playgroud)

我还在faces-config中注册了转换器,如下所示:

<converter>  
    <converter-id>booleanConverter</converter-id>  
    <converter-class>javax.faces.convert.BooleanConverter</converter-class>  
</converter>
Run Code Online (Sandbox Code Playgroud)

并试图使用它:

<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>
Run Code Online (Sandbox Code Playgroud)

但是所有这些尝试都导致了相同的行为 - 当选择空值时,它被解释为false.

我调试了它,在堆栈跟踪中我找到了它发生的位置.在AstValue.setValue(EvaluationContext, Object) line: 204

它叫

ELSupport.coerceToType(value, targetClass)
Run Code Online (Sandbox Code Playgroud)

value参数为null,targetClass为Boolean.然后,此coerceToType方法返回false.

任何想法如何解决这个问题?谢谢!

Bal*_*usC 8

这是Tomcat和JBoss使用的Apache EL解析器的典型特征.众所周知,null在EL中强制赋值时,不区分原语及其包装对象表示.包装器类型始终被视为基元.例如,它在Glassfish中工作正常.

您可以通过将以下VM参数添加到服务器启动脚本来关闭此Apache EL解析器行为:

-Dorg.apache.el.parser.COERCE_TO_ZERO=false
Run Code Online (Sandbox Code Playgroud)