我有这个 selectOneMenu 要求的东西,默认值是空的,因为人们需要考虑这个问题并且实际需要填写它。
<h:selectOneMenu id="fileSecurity" value="#{flowScope.application.fileSecurity}" required="true" >
<f:selectItem itemLabel="" itemValue="" noSelectionOption="true"/>
<f:selectItem itemLabel="ja" itemValue="true" />
<f:selectItem itemLabel="nee" itemValue="false" />
</h:selectOneMenu>
这就是我所拥有的,现在当您填写它给出的空值时:“managementTabs:ApplicationManagementForm:fileSecurity:需要填写。” 这是正确的。
当我点击"is"或它接受答案的真值时。
但是当我点击 no 或 false 时,它给出: “managementTabs:ApplicationManagementForm:fileSecurity:验证错误:值无效”
- 为什么 Jsf2.0 只接受 true 而不是 false?
- 有办法解决这个问题吗?还是我必须编写自定义验证程序?
最佳答案
提交的 false
值似乎在某种程度上与 noSelectionOption
的空字符串值冲突。第一项的以下条目工作正常:
<f:selectItem itemLabel="" itemValue="" />
和
<f:selectItem itemLabel="" noSelectionOption="true" />
和
<f:selectItem itemLabel="" itemValue="#{null}" noSelectionOption="true" />
我只是不明白为什么。这是不合逻辑的。要找到真正的答案,我需要运行调试器。
更新:我调试并在 SelectUtils#valueIsNoSelectionOption()
(Mojarra) 中找到了罪魁祸首。 noSelectionOption
的空字符串被强制转换为 boolean false
而不是 Boolean null
,因此与提交值的比较是有效的,因此被选中值无效。这是“设计使然”。您确实需要明确指定 itemValue="#{null}"
。
关于java - selectOneMenu 上的 required 是否只接受 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7387972/