我正在使用 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>
但所有这些尝试都导致了同样的行为——当选择 null 值时,它被解释为 false。
我对其进行了调试,并在堆栈跟踪中找到了它发生的位置。在AstValue.setValue(EvaluationContext, Object) line: 204
它调用
ELSupport.coerceToType(value, targetClass)
value 参数为 null,targetClass 为 Boolean。然后这个 coerceToType 方法返回 false。
有什么想法如何解决这个问题吗?谢谢!