本文介绍了如何将布尔值设置为“是"或“否" h:selectOneRadio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Tomcat 7上运行的JSF-2 Mojarra 2.0.8中具有以下代码

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>

我的ManagedBean包含以下

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;
}

如何基于

我通过在mangedbean中添加以下内容找到了工作环境

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")
    };
}

并将我的视图更改为

<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
    <f:selectItems value="#{user.myBooleanValues}" />
    <f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>

推荐答案

您也可以只检查rendered属性中的枚举值.枚举在EL中被评估为字符串,因此您可以进行简单的字符串比较.这样,您可以保留原始代码.

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.

<h:panelGrid columns="2" rendered="#{user.yesNoRadio == 'Yes'}">

顺便说一句,您可能真的想使用execute="@this"而不是execute="@form"(或者只是将其完全删除,它已经默认为@this). execute="@form"将处理 entire 表单,从而在您可能不希望的地方进行验证.

By the way, you perhaps really want to use execute="@this" instead of execute="@form" (or just remove it altogether, it defaults to @this already). The execute="@form" will process the entire form and thus also fire validation there where you perhaps don't want.

这篇关于如何将布尔值设置为“是"或“否" h:selectOneRadio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:19