【问题标题】:How to set Boolean value to an 'Yes' or 'No' h:selectOneRadio如何将布尔值设置为“是”或“否”h:selectOneRadio
【发布时间】:2012-02-08 19:57:09
【问题描述】:

我在 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>

【问题讨论】:

    标签: jsf-2


    【解决方案1】:

    在您的支持 bean 中使用一个简单的布尔值:

    private Boolean choice;
    // getter and setter
    

    在facelet中:

    <h:selectOneRadio id="yesNoRadio" value ="#{user.choice}">
      <f:selectItem itemValue="#{false}" itemLabel="No" />
      <f:selectItem itemValue="#{true}" itemLabel="Yes"/>
      <f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
    </h:selectOneRadio>
    ..
    <h:panelGrid  id="userDetailsGrid">
      <h:panelGrid columns="2" rendered="#{user.choice}">
      ..
      </h:panelGrid>
      ..
    </h:panelGrid>
    

    【讨论】:

    • 谢谢马特! ,我觉得这些直接布尔值和每个选择项的视图变得有点笨重。在公共 SelectItem[] getMyBooleanValues() 的帮助下,您是否看到上述工作方法中可能存在的任何缺点。
    【解决方案2】:

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

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

    顺便说一下,您可能真的想使用execute="@this" 而不是execute="@form"(或者干脆完全删除它,它已经默认为@this)。 execute="@form" 将处理 整个 表单,因此还会在您可能不想要的地方触发验证。

    【讨论】:

    • 是的..这就是我要找的。谢谢BalusC。我是 JSF 的新手,非常感谢您使用执行属性的 cmets。一个问题,我有命令按钮,我不希望验证被触发,所以我从命令按钮中删除了执行属性,但在这种情况下动作事件没有触发。可能是什么原因?
    猜你喜欢
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多