我正在使用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>
现在,如果我选择“任何..”,它将把“false”分配给registrationComplete字段(布尔类型)。因此null被解释为false。我还尝试在selectItem(s)中使用布尔值,即:
<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>
我还在faces-config中注册了转换器,如下所示:
<converter>
<converter-id>booleanConverter</converter-id>
<converter-class>javax.faces.convert.BooleanConverter</converter-class>
</converter>
然后尝试使用它:
<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>
但所有这些尝试都导致了相同的行为 - 当选择空值时,它被解释为假。
我进行了调试,并在堆栈跟踪中找到了发生此问题的位置。在AstValue.setValue(EvaluationContext, Object) line: 204
中调用了该位置。
ELSupport.coerceToType(value, targetClass)
当value参数为null且targetClass为Boolean时,该coerceToType方法返回false。
有什么解决这个问题的想法吗?谢谢!