如何将布尔值设置为“Yes”或“否” h:选择一个无线电
我在 Tomcat 7 上运行的 JSF-2 Mojarra 2.0.8 中有以下代码
<h:panelGrid id="yesNoRadioGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadioGridFlag}">
<h:outputText id ="otherLbl" value="Select Yes or No"></h:outputText>
<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
<f:selectItems value="#{user.choices}"/>
<f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>
</h:panelGrid>
</h:panelGrid>
<h:message for ="yesNoRadio"> </h:message>
<h:panelGrid id="userDetailsGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadio}">
<h:outputLabel>Name :</h:outputLabel>
<h:inputText id="customerName" value="#{user.customerName}"></h:inputText>
<h:outputLabel>Salary: </h:outputLabel>
<h:inputText id="customerSalary" value="#{user.customerSalary}"></h:inputText>
</h:panelGrid>
</h:panelGrid>
我的 ManagedBean 包含以下内容
private enum Choice {
Yes, No;
}
private Choice yesNoRadio;
public Choice[] getChoices() {
return Choice.values();
}
public Choice getYesNoRadio() {
return yesNoRadio;
}
public void setYesNoRadio(Choice yesNoRadio) {
this.yesNoRadio = yesNoRadio;
}
如何根据在中选择的布尔值 (user.yesNoRadio) 呈现我的“userDetailsGrid”
我通过在我的中添加以下内容找到了一个解决方法mangedbean
private Boolean yesNoRadio;
public SelectItem[] getMyBooleanValues() {
return new SelectItem[] {
new SelectItem(Boolean.TRUE, "Yes"),
new SelectItem(Boolean.FALSE, "No")
};
}
并改变我的观点
<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
<f:selectItems value="#{user.myBooleanValues}" />
<f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>
I have the following code in JSF-2 Mojarra 2.0.8 running on Tomcat 7
<h:panelGrid id="yesNoRadioGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadioGridFlag}">
<h:outputText id ="otherLbl" value="Select Yes or No"></h:outputText>
<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
<f:selectItems value="#{user.choices}"/>
<f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>
</h:panelGrid>
</h:panelGrid>
<h:message for ="yesNoRadio"> </h:message>
<h:panelGrid id="userDetailsGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadio}">
<h:outputLabel>Name :</h:outputLabel>
<h:inputText id="customerName" value="#{user.customerName}"></h:inputText>
<h:outputLabel>Salary: </h:outputLabel>
<h:inputText id="customerSalary" value="#{user.customerSalary}"></h:inputText>
</h:panelGrid>
</h:panelGrid>
My managedBean contain following
private enum Choice {
Yes, No;
}
private Choice yesNoRadio;
public Choice[] getChoices() {
return Choice.values();
}
public Choice getYesNoRadio() {
return yesNoRadio;
}
public void setYesNoRadio(Choice yesNoRadio) {
this.yesNoRadio = yesNoRadio;
}
How can I render my 'userDetailsGrid' based on the boolean value (user.yesNoRadio) selected in
I found a workarround by adding following in my mangedbean
private Boolean yesNoRadio;
public SelectItem[] getMyBooleanValues() {
return new SelectItem[] {
new SelectItem(Boolean.TRUE, "Yes"),
new SelectItem(Boolean.FALSE, "No")
};
}
and changing my view to
<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
<f:selectItems value="#{user.myBooleanValues}" />
<f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在支持 bean 中使用简单的布尔值:
在 Facelet 中:
Use a simple Boolean value in your backing bean:
And in the facelet:
您还可以仅检查
rendered
属性中的枚举值。枚举在 EL 中被计算为字符串,因此您可以进行简单的字符串比较。这样你就可以保留原来的代码。顺便说一句,您可能真的想使用
execute="@this"
而不是execute="@form"
(或者干脆将其完全删除,它默认为@this
已经)。execute="@form"
将处理整个表单,因此也会在您可能不想要的地方触发验证。You can also just check the enum value in the
rendered
attribute. Enums are evaluated as strings in EL, so you can do a simple string comparison. This way you can keep your original code.By the way, you perhaps really want to use
execute="@this"
instead ofexecute="@form"
(or just remove it altogether, it defaults to@this
already). Theexecute="@form"
will process the entire form and thus also fire validation there where you perhaps don't want.